您的位置:首页 > 产品设计 > UI/UE

java 21 : StringBuilder/StringBuffer/String

2014-07-12 16:34 375 查看
1 StringBuilder/ StringBuffer

前边我们已经有一篇详细讲解了下String,我们知道String 是一个final类,他不能被继承,方法不能被override。如果直接修改或者append String串的内容,其实已经是新建了一个String对象。所以,他有一些弊病, 不可能使用在需要频繁对字符串进行改变或者添加的情况下,这时候就应该使用StringBuffer或者StringBuilder.

The StringBuilder/StringBuffer class is an alternative to the String class. In general,

a StringBuilder/StringBuffer can be used wherever a string is used.

StringBuilder/StringBuffer is more flexible than String. You can add, insert, or

append new contents into a StringBuilder or a StringBuffer, whereas the value of a

String object is fixed, once the string is created.

The StringBuilder class is similar to StringBuffer except that the methods for mod-

ifying buffer in StringBuffer are synchronized. Use StringBuffer if it may be accessed

by multiple tasks concurrently. Using StringBuilder is more efficient if it is accessed by a

single task. The constructors and methods in StringBuffer and StringBuilder are

almost the same. This section covers StringBuilder. You may replace StringBuilder

by StringBuffer. The program can compile and run without any other changes.

StringBuidler与StringBuffer是类似的,我们都可以直接对StringBuilder/StringBuffer添加、修改、尾接一个新的串进去,依旧是同一个对象。但是他们两者之间的区别在于StringBuffer修改缓冲区的方法是同步的,所以,为了数据安全起见,如果是多线程并发访问的话,就要使用StringBuffer,否则StringBuilder是我们更好的选择,因为毕竟Stringbuffer修改是同步的必然会导致效率低一些。

由于StringBuffer/StringBuilder 是支持直接修改原数据,所以我们从API上可以看到他更多的方法是一些修改、添加、倒置等等这样操作字符串的方法,而像在String中我们不会看到这些功能的方法,这就是各施其职,各自发挥所长。

我们稍微截图看看StringBuilder都有哪些常见的方法:



另外,要知道,在java中,很多方法有(begIndex, endIndex)参数,从来endIndex不会包括在里边,例如上面倒数第三个的replace方法、delete方法等。

例子:

StringBuilder sb=new  StringBuilder();
sb.append("welcome to ");
sb.append("java");
sb.insert(11,"html and ");  //sb变成 welcome to html and java
sb.delete(8,11); //删除index 8-10 这三个字符
sb.deleteCharAt(8);
sb.reverse();


在以上的过程中,从头到尾都是对同一个对象进行,但是如果是使用String的话,每一步都会创建一个新对象、

2 toString、 capacity,length, setLength,charAt 方法

Stringbuilder除了提供上述的对字符串进行修改,添加等的操作外,同时也提供一些方法用来获取对象的一些属性的。



The capacity() method returns the current capacity of the string builder. The capacity is

the number of characters it is able to store without having to increase its size.

The length() method returns the number of characters actually stored in the stringBuilder

The setLength(newLength) method sets the length of the string builder. If the

newLength argument is less than the current length of the string builder, the string builder is

truncated to contain exactly the number of characters given by the newLength argument. If

the newLength argument is greater than or equal to the current length, sufficient null charac-

ters ('\u0000') are appended to the string builder so that length becomes the newLength

argument. The newLength argument must be greater than or equal to 0.

我们可以使用setLength方法从新设置字符串的长度,如果新设置的长度比原来实际的长度要短,那么StringBuilder会将看原来的字符串截断才新设置的长度,就好像使用了delete了后面多余的字符了。如果新设置的长度比原来实际的长,那么就利用'\u0000' 填充多出的长度。

The builder’s capacity is automatically increased if more characters are added to exceed

its capacity. Internally, a string builder is an array of characters, so the builder’s capacity is the size

of the array. If the builder’s capacity is exceeded, the array is replaced by a new array. The new

array size is 2 * (the previous array size + 1)

我们可以使用 StringBuilder sb=new StringBuilder(initCapacity) 来创建一个有initCapacity空间大小的StringBuidler方法,而当我们给一个Stringbuilder不断添加字符时候,当添加的长度已经超过了原来的capacity, builder的capacity会随着我们的添加而自动增加,在内部中,一个Stringbuilder其实是一个字符数组,所以capacity是字符数组的大小,如果大小超过了,会使用一个新的数组来替换原来的数组,将原来数组的数据给复制到新的数组中,而新的数组的capacity的大小是2*(原来的capacity+1)

所以我们应该慎重的选择我们的initCapacity,过大会造成内存浪费,过小会导致可能超过而需要有复制迁移的操作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: