使用StringBuilder类常用方法完成字符串处理例题一
任务
- 定义一个字符串“欢迎来到”
- 在“欢迎来到”后面添加内容,将字符串变成“欢迎来到CSDN”
- 使用delete方法删除“欢迎来到”,然后插入“你好!”
- 将字符串变成“你好,CSDN”
- 使用replase方法替换
package com.project.practice;
public class StringDemo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuilder str=new StringBuilder("欢迎来到");
System.out.println(str.append("CSDN"));
System.out.println("删除插入后:"+str.delete(0, 4).insert(0, "你好!").replace(2, 3, ","));
System.out.println("替换后:"+str.replace(0, 3, "欢迎来到"));
}
}
运行结果如图所示: