Stringbuffer常用方法

  1. //通过append()方法连接各种类型的数据  
  2. /*public class Append{ 
  3.     public static void main(String []args){ 
  4.         StringBuffer buf = new StringBuffer(); 
  5.         buf.append("Hello \n"); 
  6.         buf.append("Hello ").append("World!\n"); 
  7.         buf.append("数字:").append(1).append("\n");//代码链 
  8.         buf.append("布尔:").append(true); 
  9.         System.out.println(buf); 
  10.     } 
  11. }*/  
  12. /*public class Append{ 
  13.     public static void main(String []args){ 
  14.         StringBuffer buf = new StringBuffer(); 
  15.         buf.append("Hello "); 
  16.         fun(buf); 
  17.         System.out.println(buf); 
  18.     } 
  19.     public static void fun(StringBuffer s){ 
  20.         s.append("MLDN ").append("LiXingHua"); 
  21.     } 
  22. }*/  
  23. //Hello MLDN LiXingHua  
  24.   
  25. //直接使用insert()方法在指定位置上为StringBuffer添加内容  
  26. /*public class Append{ 
  27.     public static void main(String []args){ 
  28.         StringBuffer buf = new StringBuffer(); 
  29.         buf.append("Hello!!!"); 
  30.         buf.insert(0,"Hello "); 
  31.         System.out.println(buf); 
  32.         buf.insert(buf.length()," MLDN~~"); 
  33.         System.out.println(buf); 
  34.     } 
  35. }*/  
  36.   
  37. //字符串反转操作--reverse  
  38. /*public class Append{ 
  39.     public static void main(String []args){ 
  40.         StringBuffer buf = new StringBuffer(); 
  41.         buf.append("World!!!"); 
  42.         buf.insert(0,"Hello "); 
  43.         String str = buf.reverse().toString(); 
  44.         System.out.println(str); 
  45.     } 
  46. }*/  
  47.   
  48. //替换指定范围的内容--replace  
  49. /*public class Append{ 
  50.     public static void main(String []args){ 
  51.         StringBuffer buf = new StringBuffer(); 
  52.         buf.append("Hello ").append("World!!"); 
  53.         buf.replace(6,11,"LiXiHua"); 
  54.         System.out.println(buf); 
  55.     } 
  56. }*/  
  57.   
  58. //字符串截取--substring  
  59. /*public class Append{ 
  60.     public static void main(String []args){ 
  61.         StringBuffer buf = new StringBuffer(); 
  62.         buf.append("Hello ").append("World!!"); 
  63.         buf.replace(6,11,"LiXiHua"); 
  64.         String str = buf.substring(6,15); 
  65.         System.out.println(str); 
  66.     } 
  67. }*/  
  68.   
  69. //删除指定范围的字符串--delete  
  70. /*public class Append{ 
  71.     public static void main(String []args){ 
  72.         StringBuffer buf = new StringBuffer(); 
  73.         buf.append("Hello ").append("World!!"); 
  74.         buf.replace(6,11,"LiXiHua"); 
  75.         String str = buf.delete(6,15).toString(); 
  76.         System.out.println(str); 
  77.     } 
  78. }*/  
  79.   
  80. //查找指定的内容是否存在--indexOf  
  81. public class Append{  
  82.     public static void main(String []args){  
  83.         StringBuffer buf = new StringBuffer();  
  84.         buf.append("Hello ").append("World!!");  
  85.         if(buf.indexOf("Hello")==-1){  
  86.             System.out.println("没有找到指定内容");  
  87.         }else{  
  88.             System.out.println("可以找到指定内容");  
  89.         }  
  90.     }  
  91. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值