• 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. mybatis学习:mybatis的注解开发和编写dao实现类的方式入门

    一.使用注解则不需要创建映射配置文件:即xxxDao.xml javaBean为什么要实现Serializable接口? Java的"对象序列化"能让你将一个实现了Serializ ...

  2. HTML,CSS,JS优化

    HTML部分 语义化HTML:好处在于可以使代码简洁清晰,支持不同设备,利于搜索引擎,便于团队开发: 减少DOM节点:加速页面渲染: 给图片加上正确的宽高值:这可以减少页面重绘,同时防止图片缩放: 防 ...

  3. call(this)自记

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. python使用cPickle模块序列化实例

    python使用cPickle模块序列化实例 这篇文章主要介绍了python使用cPickle模块序列化的方法,是一个非常实用的技巧,本文实例讲述了python使用cPickle模块序列化的方法,分享 ...

  5. php解决高并发(文件锁)

    文件锁分为两种方式: [一].阻塞模式:(如果其他进程已经加锁文件,当前进程会一直等其他进程解锁文件后继续执行) <?php //连接数据库 $con=mysqli_connect(" ...

  6. .net4.6版本前设置window子窗口位置主窗口闪烁

    在安装了.net4.6的版本是不会出现该问题的,但是在4.6以下的版本会出现,当设置之窗体的left和top属性时,会让主窗体闪烁一下. 之前是在load事件下写的: child_window.loa ...

  7. Python服务端工程师就业面试指导

    Python服务端工程师就业面试指导 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大家看的时候 ...

  8. day37 02-Hibernate二级缓存:二级缓存的散装数据

    一级缓存存放的是对象的地址.把对象的地址缓存下来了.二级缓存里面存放的是对象的散装数据.你再去获取的时候,因为一级缓存的生命周期结束了,它会从二级缓存中获取.从二级缓存中获取,因为它又会得到一个对象. ...

  9. Struts_添加客户练习

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

  10. 洛谷P2429 制杖题 [2017年6月计划 数论10]

    P2429 制杖题 题目描述 求不大于 m 的. 质因数集与给定质数集有交集的自然数之和. 输入输出格式 输入格式: 第一行二个整数 n,m. 第二行 n 个整数,表示质数集内的元素 p[i]. 输出 ...