标签:can print 子串 stat ringbuf void ack replace char
package Main;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("--------1.添加--------");
add();
System.out.println("--------2.删除--------");
remove();
System.out.println("--------3.修改--------");
alter();
}
public static void add() {
StringBuffer sb=new StringBuffer();
sb.append("abcdefg"); //在末尾追加子串
System.out.println("append的添加结果"+sb);
sb.insert(2, "123"); //在指定位置插入字符串
System.out.println("insert的添加结果:"+sb);
}
public static void remove() {
StringBuffer sb=new StringBuffer("abcdefg");
sb.delete(1, 5); //在指定范围删除字符串
System.out.println("delete的删除结果:"+sb);
sb.deleteCharAt(2); //在指定位置删除字符
System.out.println("deleteCharAt的删除结果:"+sb);
sb.delete(0, sb.length()); //将整个字符串清除
System.out.println(sb);
}
public static void alter() {
StringBuffer sb=new StringBuffer("abcdefg");
sb.setCharAt(1, ‘p‘); //修改指定位置的字符
System.out.println("setCharAt修改指定字符的结果:"+sb);
sb.replace(1, 3, "qq"); //替换指定位置的字符或者字符串
System.out.println("replace替换字符串的结果:"+sb);
System.out.println("reverse逆序字符串的结果:"+sb.reverse());
}
}
标签:can print 子串 stat ringbuf void ack replace char
原文地址:https://www.cnblogs.com/julyzqy/p/12777382.html