StringBuffer的常用方法 – JAVA編程語言程序開發技術文章

[java] 
//通過append()方法連接各種類型的數據 
/*public class Append{
    public static void main(String []args){
        StringBuffer buf = new StringBuffer();
        buf.append("Hello \n");
        buf.append("Hello ").append("World!\n");
        buf.append("數字:").append(1).append("\n");//代碼鏈
        buf.append("佈爾:").append(true);
        System.out.println(buf);
    }
}*/ 
/*public class Append{
    public static void main(String []args){
        StringBuffer buf = new StringBuffer();
        buf.append("Hello ");
        fun(buf);
        System.out.println(buf);
    }
    public static void fun(StringBuffer s){
        s.append("MLDN ").append("LiXingHua");
    }
}*/ 
//Hello MLDN LiXingHua 
 
//直接使用insert()方法在指定位置上為StringBuffer添加內容 
/*public class Append{
    public static void main(String []args){
        StringBuffer buf = new StringBuffer();
        buf.append("Hello!!!");
        buf.insert(0,"Hello ");
        System.out.println(buf);
        buf.insert(buf.length()," MLDN~~");
        System.out.println(buf);
    }
}*/ 
 
//字符串反轉操作–reverse 
/*public class Append{
    public static void main(String []args){
        StringBuffer buf = new StringBuffer();
        buf.append("World!!!");
        buf.insert(0,"Hello ");
        String str = buf.reverse().toString();
        System.out.println(str);
    }
}*/ 
 
//替換指定范圍的內容–replace 
/*public class Append{
    public static void main(String []args){
        StringBuffer buf = new StringBuffer();
        buf.append("Hello ").append("World!!");
        buf.replace(6,11,"LiXiHua");
        System.out.println(buf);
    }
}*/ 
 
//字符串截取–substring 
/*public class Append{
    public static void main(String []args){
        StringBuffer buf = new StringBuffer();
        buf.append("Hello ").append("World!!");
        buf.replace(6,11,"LiXiHua");
        String str = buf.substring(6,15);
        System.out.println(str);
    }
}*/ 
 
//刪除指定范圍的字符串–delete 
/*public class Append{
    public static void main(String []args){
        StringBuffer buf = new StringBuffer();
        buf.append("Hello ").append("World!!");
        buf.replace(6,11,"LiXiHua");
        String str = buf.delete(6,15).toString();
        System.out.println(str);
    }
}*/ 
 
//查找指定的內容是否存在–indexOf 
public class Append{ 
    public static void main(String []args){ 
        StringBuffer buf = new StringBuffer(); 
        buf.append("Hello ").append("World!!"); 
        if(buf.indexOf("Hello")==-1){ 
            System.out.println("沒有找到指定內容"); 
        }else{ 
            System.out.println("可以找到指定內容"); 
        } 
    } 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。