String str = "finished with exit finished";
//replace
System.out.println(str.replace('i', 'a'));
System.out.println(str.replaceFirst("fi", "ti"));
System.out.println(str.replaceAll("i", "a"));
//fanashed wath exat fanashed
//tinished with exit finished
//fanashed wath exat fanashed
//截取字符串
System.out.println(str.substring(5));
System.out.println(str.substring(2, 5));
// result: hed with exit finished
//result: nis
//字符串反转
String reverse = new StringBuffer(str).reverse().toString();
System.out.println("字符串反转后:" + reverse);
//result: 字符串反转后:dehsinif tixe htiw dehsinif
//字符串查找
int intIndex = str.lastIndexOf("exit");
if (intIndex == -1) {
System.out.println("没有找到字符串: exit");
} else {
System.out.println("exit字符串位置为:" + intIndex);
}
//result: exit字符串位置为:14
//查找字符串最后一次出现的位置
int lastIndex = str.lastIndexOf("exit");
if (lastIndex == -1) {
System.out.println("没有找到字符串: exit");
} else {
System.out.println("exit字符串最后出现的位置为:" + lastIndex);
}
//result:14
//字符串小写转大写
String capitalLetters = str.toUpperCase();
System.out.println(capitalLetters);
//result: FINISHED WITH EXIT FINISHED
// 删除字符串中的一个字符
System.out.println(removeCharAt(str, 7));
}
private String removeCharAt(String str, int i) {
return str.substring(0, 7) + str.substring(i + 1);
//result: finishe with exit finished