总结StringBuffer类的使用方法,文章转载自
http://www.cnblogs.com/yangyquin/p/4943751.html,并做了部分梳理
StringBuffer构造器
- 无参数:StringBuffer()
- 指定capacity大小:StringBuffer(int capacity)
- 指定内容:StringBuffer(String str)
package stringBuffer;
public class Demo
{
public static void main(String[] args)
{
//Creator
StringBuffer strA = new StringBuffer();
//test
//System.out.println("Creator with no parameter");
System.out.println("\tstrA's address in memeroy: " + strA);
System.out.println("\tstrA's capacity is: " + strA.capacity());
System.out.println("\tstrA's length is: " + strA.length());
StringBuffer strB = new StringBuffer(10);
//test
//System.out.println("Creator with fixed capacity");
System.out.println("\tstrB's address in memeroy: " + strB);
System.out.println("\tstrB's capacity is: " + strB.capacity());
System.out.println("\tstrB's length is: " + strB.length());
StringBuffer strC = new StringBuffer("hello");
//test
//System.out.println("Creator with string");
System.out.println("\tstrC's address in memeroy: " + strC);
System.out.println("\tstrC's capacity is: " + strC.capacity());
System.out.println("\tstrC's length is: " + strC.length());
}
}
- 源码如下所示,注意构造器默认capacity为16:
/**
* Constructs a string buffer with no characters in it and an
* initial capacity of 16 characters.
*/
public StringBuffer() {
super(16);
}
/**
* Constructs a string buffer with no characters in it and
* the specified initial capacity.
*
* @param capacity the initial capacity.
* @exception NegativeArraySizeException if the <code>capacity</code>
* argument is less than <code>0</code>.
*/
public StringBuffer(int capacity) {
super(capacity);
}
/**
* Constructs a string buffer initialized to the contents of the
* specified string. The initial capacity of the string buffer is
* <code>16</code> plus the length of the string argument.
*
* @param str the initial contents of the buffer.
* @exception NullPointerException if <code>str</code> is <code>null</code>
*/
public StringBuffer(String str) {
super(str.length() + 16);
append(str);
}
StringBuffer类成员函数使用
添加功能
- public StringBuffer append(type data):可以把任意类型数据添加到字符串缓冲区中,并返回字符串本身
- public StringBuffer insert(int offset,type data):在指定位置把任意字符串的数据插入到字符串缓冲区,并返回字符串本身
package stringBuffer;
public class DemoOne
{
public static void main(String[] args)
{
//Create StringBuffer with no parameter
StringBuffer strA = new StringBuffer();
//return itself
StringBuffer strB = strA.append("hello");
System.out.println("strA == strB ? " + (strA == strB));
//add data one by one
StringBuffer strC = new StringBuffer();
strC.append("hello");
strC.append(true);
strC.append(12);
strC.append(34.56);
System.out.println("strC: " + strC);
//Chain Programming
StringBuffer strD = new StringBuffer();
strD.append("hello").append(true).append(12).append(34.56);
System.out.println("strD:" + strD);
strC.insert(5,"hello");
System.out.println("strC after insert: " + strC);
}
}
删除功能
- public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回字符串本身
- public StringBuffer delete(int start,int end):删除从指定位置开始,指定位置结束的内容,并返回本身
/**
* Created on 1/5/17.
*/
public class DemoThree {
public static void main(String[] args)
{
//Create object
StringBuffer strA = new StringBuffer();
strA.append("hello world java");
System.out.println("strA: " + strA);
//delete element
strA.deleteCharAt(1);
System.out.println("strA after deleteCharAt(1): " + strA);
//delete elements in range
strA.delete(5,10);
System.out.println("StrA after delete(5,10)");
}
}
替换功能
- public StringBuffer replace(int start,int end,String str):使用给定String中的字符串替换序列的子字符串中的字符
public class DemoFour {
public static void main(String[] args)
{
//create object
StringBuffer strA = new StringBuffer("hello world java");
System.out.println("strA: " + strA);
//replace
strA.replace(6,10,"happy New Year");//左闭右开区间
System.out.println("strA after replace: " + strA);
}
}
###反转功能
- public StringBuffer reverse():将此字符串反转并返回字符串本身
public class DemoFive {
public static void main(String[] args)
{
//create object
StringBuffer strA = new StringBuffer("Tommy love me");
//reverse data
strA.reverse();
System.out.println("strA after reverse(): " + strA);
}
截取功能
- public String substring(int start)
- public String substring(int start,int end)
public class DemoSix {
public static void main(String[] args)
{
//create object
StringBuffer strA = new StringBuffer("hello world java");
System.out.println("strA: " + strA);
//cuttln
String strB = strA.substring(5);
System.out.println("strB: " + strB);
String strC = strA.substring(5,10);
System.out.println("strC: " + strC);
}
}