• String 对象是不可修改的,对于被String 重载的'+' 和'+=' 运算符来说,当你用它们来连接两个String 对象的时候,它在底层并不会对于每一次连接均生成一个String 对象,取而代之,在底层会有一个非线程安全的可修改的StringBuilder 对象被构造出来,并且调用其append()方法来生成我们需要拼接的字符串。关于这一点,我们可以随便编写一个字符串拼接语句,然后使用JDK自带的javap 工具,通过javap -c 编译后的类文件名 命令来查看其对应的JVM字节码(相当于汇编语言)来得到一些启示。不过,应当注意的一点是,在循环语句块中拼接字符串,在每次循环的过程中都会生成一个新的StringBuilder 对象(即该对象的构造发生在循环的内部),因此,如果你关注程序的性能,那么这种情况下显式地使用StringBuilder 对象的append 方法是很有必要的。不过,像append(a + ":" + c) 这种语句,对于内部的连接语句,编译器又会帮你生成一个新的StringBuilder 对象,这种情况下可以分步append:append(a).append(":").append(c)。
  • StringBuilder was introduced in Java SE5. Prior to this, Java used StringBuffer, which ensured thread safety and so was significantly more expensive. Thus, string operations in Java SE5/6 should be faster.
  • 如果重写了某个类的toString 方法,那么不要在其中单纯地使用this 拼接字符串,因为这会导致程序无限递归调用该类的toString 方法,最终导致栈溢出。如果想要获取该对象的地址,可以调用super.toString() 或者Integer.toHexString(this.hashCode()) 来获得。
  • Formatter provides powerful control over spacing and alignment with fairly concise notation.

Regular expressions

  • In general, you'll compile regular expression objects rather than using the fairly limited String utilities. To do this, you import java.util.regex, then compile a regular expression by using the static Pattern.compile() method. This produces Pattern object based on its String argument. You use the Pattern by calling the matcher() method, passing the string that you want to search. The matcher() method produces a Matcher object, which has a set of operations to choose from(you can see all of these in the JDK documentation for java.util.regex.Matcher). For example, the replaceAll() method replaces all the matches with its argument.
  • find() is like an iterator, moving forward through the input string.
  • Groups are regular expressions set off by parentheses that can be called up later with their group number. Group 0 indicates the whole expression match, group 1 is the first parenthesized group, etc. Thus in A(B(C))D, There are three groups: Group 0 is ABCD, group 1 is BC, and group 2 is C.

TIJ——Chapter Thirteen:Strings的更多相关文章

  1. TIJ——Chapter Two:Everything Is an Object

    If we spoke a different language, we would perceive a somewhat different world. Ludwig Wittgenstein( ...

  2. TIJ——Chapter Eleven:Holding Your Objects

    Java Provides a number of ways to hold objects: An array associates numerical indexes to objects. It ...

  3. TIJ——Chapter One:Introduction to Objects

    ///:~容我对这个系列美其名曰"读书笔记",其实shi在练习英文哈:-) Introduction to Objects Object-oriented programming( ...

  4. Think Python - Chapter 8 - Strings

    8.1 A string is a sequenceA string is a sequence of characters. You can access the characters one at ...

  5. TIJ——Chapter Twelve:Error Handling with Exception

    Exception guidelines Use exceptions to: Handle problems at the appropriate level.(Avoid catching exc ...

  6. TIJ——Chapter Eight:Polymorphism

    The twist |_Method-call binding Connecting a method call to a method body is called binding. When bi ...

  7. TIJ——Chapter Seven:Reusing Classes

    Reusing Classes 有两种常用方式实现类的重用,组件(在新类中创建存在类的对象)和继承. Composition syntax Every non-primitive object has ...

  8. TIJ——Chapter Five:Initialization & Cleanup

    Method overloading |_Distinguishing overloaded methods If the methods hava the same name, how can Ja ...

  9. TIJ——Chapter Fourteen:Type Information

    Runtime type information(RTTI) allows you to discover and use type information while a program is ru ...

随机推荐

  1. oracle基础学习(1)

    -解锁用户,需要使用dba权限conn sys/1234 as dba; alert user scott account unlock;/ --initcap方法,实现字符串首字符大写,其余字符小写 ...

  2. Leetcode434.Number of Segments in a String字符串中的单词数

    统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my name is John" ...

  3. 浅谈java.util.ConcurrentModificationException(并发修改异常)

    java中的list集合是我们经常使用的集合,而对集合进行增加和删除元素是我们最常用的操作.那么在什么时候对list集合什么样的操作,就会发生java.util.ConcurrentModificat ...

  4. [Array] 566. Reshape the Matrix

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...

  5. Python子进程 (subprocess包)

    Python子进程 (subprocess包) subprocess以及常用的封装函数 当我们运行python的时候,我们都是在创建并运行一个进程.正如我们在Linux进程基础中介绍的那样,一个进程可 ...

  6. java开发岗位面试整理

    一.Java基础 1. String类为什么是final的 2. HashMap的源码,实现原理,底层结构. 3. 说说你知道的几个Java集合类:list.set.queue.map实现类. 4. ...

  7. 在Mac下安装MySQL

    在Mac下安装MySQL   最近开始将开发工具都转移到 Mac 上了,其中也会莫名其妙的遇到一些坑,不如干脆将整个流程都记录下来,方便以后查找. 下载与安装 首先进入 MySQL 官网,选择免费的C ...

  8. Struts_添加客户练习

    1.修改CustomerAction,实现ModelDriven接口 2.修改配置文件 3.修改表单提交地址

  9. Vue表单验证插件的制作过程

    一.表单验证模块的构成 任何表单验证模块都是由 配置――校验――报错――取值 这几部分构成的. 配置: 配置规则 和配置报错,以及优先级 校验: 有在 change 事件校验, 在点击提交按钮的时候校 ...

  10. mybatis官网文档mybatis_doc

    在平时的学习中,我们可以去参考官网的文档来学习,这个文档有中文的,方便我们去阅读,而且这里的分类很详细. 官网文档链接:http://www.mybatis.org/mybatis-3/zh/inde ...