- //通过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("可以找到指定内容");
- }
- }
- }
Stringbuffer常用方法
最新推荐文章于 2024-05-13 13:21:36 发布