//StringBuffer的测试
/**
*<p>项目名称: Java</p>
*<p>文件名称: StringBufferTest</p>
*<p>版权所有: 版权所有(C)2008-2010</p>
*<p>公 司:
*<p>编写日期: 2008-7-15上午08:46:43</p>
*<p>作 者: huangtao</p>
*/
/**
* @author huangtao
*
*/
public class StringBufferTest
{
/**
* @param args
*/
public static void main(String[] args)
{
char[] chone = new char[]{'1' , '2' , '3' , '4'};
//产生的StringBuffer对象的方式
StringBuffer sbone = new StringBuffer();
//StringBuffer的附加方法的使用
System.out.println(sbone.append(new Boolean("true").booleanValue()));
System.out.println(sbone.append('9'));
System.out.println(sbone.append(chone));
System.out.println(sbone.append(new Double("10D").doubleValue()));
System.out.println(sbone.append(new Float("5F").floatValue()));
System.out.println(sbone.append(new Integer("10").intValue()));
System.out.println(sbone.append(new Long("4").longValue()));
System.out.println(sbone.append(new Person("huangtao" , 24)));
System.out.println(sbone.append(new String("how are you")));
System.out.println(sbone.append(new StringBuffer("beijing")));
System.out.println(sbone.append(chone , 0 , 2));
//返回当前容量
StringBuffer sbtwo = new StringBuffer("2008");
System.out.println(sbtwo.capacity());
System.out.println(new StringBuffer().capacity());
//返回某个索引处的字符
StringBuffer sbthree = new StringBuffer("0123456");
for(int i = 0 ; i<sbthree.length() ; i++)
{
System.out.println(sbthree.charAt(i));
//返回某个索引处的unicode码
System.out.println(sbthree.codePointAt(i));
}
//返回某个索引处的unicode码的个数
System.out.println(sbthree.codePointCount(0 , sbthree.length()));
System.out.println(sbthree.codePointBefore(7));
//删除某个索引开始处的字符序列
System.out.println(sbthree.delete(0 , 3));//3456
System.out.println(sbthree.append("012"));//3456012
//删除某个索引处的字符
System.out.println(sbthree.deleteCharAt(6));
sbthree.ensureCapacity(16);
//转换成一个字符数组
StringBuffer sbfour = new StringBuffer("2008");
char[] chtwo = new char[10];
sbfour.getChars(0 , sbfour.length() , chtwo , chtwo.length-sbfour.length());
for(int i = 0 ; i<chtwo.length ; i++)
{
System.out.println(chtwo[i]);
}
//合理分配StringBuffer的容量
sbfour.trimToSize();
//截取子字符串
System.out.println(sbfour.substring(0 , 3));
System.out.println(sbfour.substring(3));
System.out.println(sbfour.reverse());
System.out.println(sbfour.length()+"_"+sbfour.substring(0));
//更改某个索引处的字符
sbfour.setCharAt(sbfour.length()-1 , '9');
System.out.println(sbfour.length()+"_"+sbfour.substring(0));
//用指定字符串替换某个范围内的字符串
StringBuffer sbsix = new StringBuffer("h123h123hhbeijing");
System.out.println(sbsix.replace(0+4 , sbsix.length() , "999"));
//在指定索引处插入数据
StringBuffer sbseven=new StringBuffer("2008");
System.out.println(sbseven.insert(0 , new char[]{'c','h','i','n','a'} , 0 , new char[]{'c','h','i','n','a'}.length));
System.out.println(sbseven.insert(sbseven.indexOf("2") , "beijing"));
sbseven.trimToSize();
System.out.println(sbseven.insert(0 , "中国北京"));
}
}
class Person
{
int age;
String name;
Person(String name , int age)
{
this.age = age;
this.name = name;
}
public String toString()
{
return "name="+name+",age="+age;
}
}