class StringBufferExample
{
public static void main(String args[ ])
{
StringBuffer str=new StringBuffer("ABCDEFG");
str.append("123456789"); // 向str尾加“123456789”。
System.out.println(str);
str.setCharAt(1,'b'); // 將str中的字符 ‘B’替換爲‘b’。
System.out.println(str);
str.insert(0,"Game"); // 在str中的“123456789”前面插入“Game”。
System.out.println(str);
int index=str.indexOf("1"); // 獲取str中首次出現“1”的位置。
str.delete(11,15); // 刪除str中“1234”。
int n=str.length(); // 獲取str中字符個數。
str.replace(13,16,"七八九"); // 將str中“789”替換爲“七八九”。
System.out.println(str);
StringBuffer otherStr=new StringBuffer("we love you");
int start=0;
char c='/0';
while(start!=-1)
{
if(start!=0)
{
start=start+1;
}
c=otherStr.charAt(start);
if(Character.isLowerCase(c))
{
c=Character.toUpperCase(c);
otherStr.setCharAt(start,c);
}
start=otherStr.indexOf(" ",start); //查找下一個空格。
}
System.out.println(otherStr);
StringBuffer yourStr=new StringBuffer("i Love THIS GaME");
for(int i = 0; i < yourStr.length(); i++)
{
char ch = yourStr.charAt(i);
if(Character.isLowerCase(ch))
{
ch=Character.toUpperCase(ch);
}
else
{
ch=Character.toLowerCase(ch);
}
}
// 將yourStr中的大寫字符替換爲對應的小寫字符,
// 小寫字符替換爲對應的大寫字符
System.out.println(yourStr);
}
}