2021-03-14

java.lang.StringBuffer 的常用接口方法

1 : append(char c)
将 char参数的字符串表示附加到此序列。

public class String01Test {
    public static void main(String[] args) {
        StringBuffer thing = new StringBuffer("hello");
        String str = " world!";
        thing.append(str);
        System.out.println(thing);
    }
}

运行结果

hello world!

2 : insert(int up, char c)
在此序列中插入 char参数的字符串表示形式。 (第一个参数是表示从该位置后进行插入)

public class String01Test {
    public static void main(String[] args) {
        StringBuffer thing = new StringBuffer("hello");
        String str = " world!";
        thing.insert(0,str);
        System.out.println(thing);
    }
}

运行结果

 world!hello

3 : lenth();
返回长度(字符数)。

public class String01Test {
    public static void main(String[] args) {
        StringBuffer thing = new StringBuffer("hello");
        int num = thing.length();
        System.out.println(num);
    }
}

运行结果

5

4 : reverse()
导致该字符序列被序列的相反代替。

public class String01Test {
    public static void main(String[] args) {
        StringBuffer thing = new StringBuffer("hello");
        thing.reverse();
        System.out.println(thing);
    }
}

运行结果

olleh

5 : delete(int start,int end)
删除此序列的子字符串中的字符 从start到end序列的字符串会被删除

public class String01Test {
    public static void main(String[] args) {
        StringBuffer thing = new StringBuffer("hello");
        thing.delete(0,3);
        System.out.println(thing);
    }
}

运行结果

lo

以上为StringBuffer类常用的接口方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值