62,612
社区成员
发帖
与我相关
我的任务
分享
public class Test{
public static void main(String[] args){
String str = "Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak";
System.out.println(str);
System.out.println(str.replaceAll("(^.*)(?<=\\b)two(?=\\b)","$1Two"));
}
}
String str = "Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak";
StringBuffer sb = new StringBuffer(str);
int index = str.lastIndexOf(" two ");
char c = str.charAt(index + 1);
String ss = String.valueOf(c).toUpperCase();
sb = sb.replace(index + 1, index + 2, ss);
System.out.println(sb.toString());
String str = "Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak";
int index = str.lastIndexOf(" two ");
String str1 = str.substring(0, index + 1);
String str2 = str.substring(index + 1, index + 2);
String str3 = str.substring(index + 2);
String newStr = str1.concat(str2.toUpperCase()).concat(str3);
System.out.println(newStr);
public static void main(String[] args) {
String word = "two", target = "Two";
String str = "Nature has given us that two ears, two eyes, and but...";
StringBuilder stringBuilder = new StringBuilder(str.length());
stringBuilder.append(str);
int index = str.lastIndexOf(word);
stringBuilder = stringBuilder.replace(index, index + word.length(), target);
System.out.println(stringBuilder.toString());
}