StringBuffer and StringBuilder

来源:互联网 发布:java 抽象类继承接口 编辑:程序博客网 时间:2024/06/08 06:37

1, StringBuffer构造方法:

1) public StringBuffer()Constructs a string buffer with no characters in it and an initial capacity of 16 characters.(2) public StringBuffer(int capacity)Constructs a string buffer with no characters in it and the specified initial capacity.(3) public StringBuffer(String str)Constructs a string buffer initialized to the contents of the specified string. The initial capacity of the string buffer is 16 plus the length of the string argument.

2, StringBuffer添加功能:

1public StringBuffer append(String str)Appends the specified string to this character sequence. (2)public StringBuffer insert(int offset, String str)Inserts the string into this character sequence.

3, StringBuffer删除功能:

1)public StringBuffer delete(int start,int end)Removes the characters in a substring of this sequence.(和其他方法一样,包括头部,不含尾部)(2delete删除不存在的字符会索引越界异常。

4, StringBuffer替换和反转功能:

1)替换:public StringBuffer replace(int start, int end, String str)(2)反转:public StringBuffer reverse()

5, StringBuffer截取功能:

public String substring(int start)

6, The transform between String object and stringBuffer object

String to StringBuffer

1) 使用append方法,因为其return StringBuffer;(2) 使用构造方法,public StringBuffer(String str)

StringBuffer to String

1) 使用String构造方法public String(StringBuffer buffer)(2) 使用toString方法public String toString()(3) 使用subString(0public String substring(int start,int end)

总结

StringBuffer:

字符串缓存区,线程安全同步,可以对原字符串序列进行修改,构造一个其中不带字符的字符串缓冲区,初始容量为 16 个字符,始于JDK1.0;特点: 是一个容器,长度可变,缓冲区中可以存储任意类型的数据,最终需要变成字符串

StringBuilder:
同为字符串缓存区,线程不安全,效率高,可以对字符串内容进行修改,始于JDK1.5。构造一个其中不带字符的字符串生成器,初始容量为 16 个字符。该类被设计用作 StringBuffer 的一个简易替换,方法和StringBuffer一样;

String:原字符序列不可改变

Private happiness toMyself(String[] hardworking,long dream ) {        Always remember:        "I am worthy of all the best in life and I can do anything I dream of doing!"}
0 0
原创粉丝点击