StringBuilder的用法


public class List1<T> {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("hello");
        System.out.println(sb);
        sb.append("world");
        System.out.println(sb);
        System.out.println(sb.reverse());//字符串颠倒
        System.out.println(sb.delete(2,6));//删除2-6的字符串不包括6
        System.out.println(sb.insert(3,8));//在第三的位置插入8
        System.out.println(sb.insert(3,"H"));//在3的位置插入H
        System.out.println(sb.replace(1,3,"w"));//把1-3的字符串替换为w
        System.out.println(sb.capacity());//返回字符串的容量
        System.out.println(sb.charAt(6));//返回位置6的字符串
        System.out.println(sb.indexOf("h"));//返回h的位置
        System.out.println(sb.indexOf("H",2));//从2位置开始搜索返回l的位置
        System.out.println(sb.lastIndexOf("w"));//从后往前找第一个w的位置
        sb.setCharAt(2,'h');//将位置2的字符展缓为h
        System.out.println(sb);//输出
        System.out.println(sb.substring(2,7));//截取2-7之间的字符串不包括7

    }
}
输出结果:
hello
helloworld
dlrowolleh
dllleh
dll8leh
dllH8leh
dwH8leh
21
h
6
2
1
dwh8leh
h8leh

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值