java_字符串Demo2

本文详细介绍了Java中的StringBuffer和StringBuilder类,包括append、insert、toString、reverse、deleteCharAt、delete、replace和setCharAt等核心方法的使用示例及功能演示。
摘要由CSDN通过智能技术生成
package String;

public class Demo1 {
    public static void main(String[] args) {
        //StringBuffer StringBuilder
        //1.创建
        StringBuffer stringBuffer=new StringBuffer("Hello world");
        StringBuffer stringBuffer1=new StringBuffer();

        //方法
        //1.append():在末尾添加
        System.out.println("1.append():在末尾添加:");
        StringBuffer stringBuffer2=new StringBuffer();
        stringBuffer2.append("123");
        System.out.println(stringBuffer2);
        System.out.println("===");

        //2.insert(int index,String s):在指定位置插入string
        System.out.println("2.insert(int index,String s):在指定位置插入string:");
        StringBuffer stringBuffer3=new StringBuffer("abc");
        stringBuffer3.insert(1,"123");
        System.out.println(stringBuffer3);
        System.out.println("===");

        //3.toString:返回为字符串对象
        System.out.println("3.toString:返回为字符串对象:");
        StringBuffer stringBuffer4=new StringBuffer("Hello123");
        String s=stringBuffer.toString();
        System.out.println(s);
        System.out.println("===");

        //4.reverse():字符串反转
        System.out.println("4.reverse():字符串反转:");
        StringBuffer stringBuffer5=new StringBuffer("Hello123");
        stringBuffer5.reverse();
        System.out.println(stringBuffer5);
        System.out.println("===");

        //5.deleteCharAt(int index):删除指定位置的字符
        System.out.println("5.deleteCharAt(int index):删除指定位置的字符:");
        StringBuffer stringBuffer6=new StringBuffer("123456H");
        stringBuffer6.deleteCharAt(0);
        System.out.println(stringBuffer6);
        System.out.println("===");

        //6.delete(int start,int end):删除指定范围的字符串
        System.out.println("6.delete(int start,int end):删除指定范围的字符串:");
        StringBuffer stringBuffer7=new StringBuffer("123456F");
        stringBuffer7.delete(0,1);//[start,end)
        System.out.println(stringBuffer7);
        System.out.println("===");

        //7.replace(int start,int end,String):替换
        System.out.println("7.replace(int start,int end,String):");
        StringBuffer stringBuffer8=new StringBuffer("Hello World");
        stringBuffer8.replace(0,stringBuffer8.length(),"999");//[start,end]
        System.out.println(stringBuffer8);
        System.out.println("===");

        //8.setCharAt(int index,char c):修改
        System.out.println("8.setCharAt(int index,char c):修改:");
        StringBuffer stringBuffer9=new StringBuffer("12345F");
        stringBuffer9.setCharAt(0,'0');
        System.out.println(stringBuffer9);
        System.out.println("===");
    }
}


1.append():在末尾添加:
123
===
2.insert(int index,String s):在指定位置插入string:
a123bc
===
3.toString:返回为字符串对象:
Hello world
===
4.reverse():字符串反转:
321olleH
===
5.deleteCharAt(int index):删除指定位置的字符:
23456H
===
6.delete(int start,int end):删除指定范围的字符串:
23456F
===
7.replace(int start,int end,String):
999
===
8.setCharAt(int index,char c):修改:
02345F
===

Process finished with exit code 0
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值