我必须使用字符串方法找出字符串中单词的倒数第二次出现:
String input = "Hi Afternoon Hello Afternoon Hi Afternoon GM Afternoon Hi Afternoon";
输出应为
Output = Hi Afternoon Hello Afternoon Hi Afternoon GM GoodNight Hi Afternoon
你能在下面的代码中提供帮助吗?不知怎么的,它整个下午都被晚安取代了:
System.out.println(input.lastIndexOf("Afternoon"));
String subString =input.substring(0,input.lastIndexOf("Afternoon")-1);
System.out.println(subString);
System.out.println("substrint total length " + subString.length());
System.out.println("Last index of "+subString.lastIndexOf("Afternoon"));
String replaceString = subString.substring(subString.lastIndexOf("Afternoon"), 50);
System.out.println(subString.replace(replaceString, "GoodNight"));
一种方法是使用显示的子字符串和其他答案。
如果你觉得生活在边缘,你可以使用正则表达式替换,利用一行的反向引用:
input.replaceFirst("(.*)(Afternoon)(.*)(?=(2))", "$1GoodNight$3");
这将满足以下情况:
Input: Hi Afternoon Hello Afternoon Hi Afternoon GM Afternoon Hi Afternoon
//Hi Afternoon Hello Afternoon Hi Afternoon GM GoodNight Hi Afternoon
Input: Hi Afternoon Hello Afternoon Hi Afternoon GM Afternoon Hi
//Hi Afternoon Hello Afternoon Hi GoodNight GM Afternoon Hi
Input: Hi Afternoon Hello Afternoon Hi Afternoon GM
//Hi Afternoon Hello GoodNight Hi Afternoon GM
Input: Hi Afternoon Hello Afternoon
//Hi GoodNight Hello Afternoon
Input: Hi Afternoon Hello
//Hi Afternoon Hello
Input: Hi Afternoon
//Hi Afternoon
Input: Hi Hello
//Hi Hello
要查找倒数第二个索引,请首先调用input.lastIndexOf(String str)
,然后调用input.lastIndexOf(String str, int fromIndex)
,传入第一个调用的值。
现在可以使用input.substring(int beginIndex, int endIndex)
和input.substring(int beginIndex)
构建结果。
static String replaceSecondLast(String input, String search, String replacement) {
int lastIdx = input.lastIndexOf(search);
int secondLastIdx = (lastIdx <= 0 ? -1 : input.lastIndexOf(search, lastIdx - 1));
if (secondLastIdx < 0)
return input;
return input.substring(0, secondLastIdx) + replacement +
input.substring(secondLastIdx + search.length());
}
测试
System.out.println(replaceSecondLast(
"Hi Afternoon Hello Afternoon Hi Afternoon GM Afternoon Hi Afternoon",
"Afternoon", "GoodNight"));
输出
Hi Afternoon Hello Afternoon Hi Afternoon GM GoodNight Hi Afternoon
String#substring(int beginIndex)
按如下操作:
public class Main {
public static void main(String[] args) {
String input = "Hi Afternoon Hello Afternoon Hi Afternoon GM Afternoon Hi Afternoon";
// Get the substring without the 'Afternoon'
String substr1 = input.substring(0, input.lastIndexOf("Afternoon"));
// Get the substring without the second last 'Afternoon'
String substr2 = substr1.substring(0, substr1.lastIndexOf("Afternoon"));
// Create the final string by replacing the second last 'Afternoon' with
// 'GoodNight' and reinstating the remaining substrings as they are
String finalStr = substr2 + "GoodNight"
+ input.substring(substr1.lastIndexOf("Afternoon") + "Afternoon".length());
System.out.println(finalStr);
}
}
输出:
Hi Afternoon Hello Afternoon Hi Afternoon GM GoodNight Hi Afternoon
[更新]由Andreas 提供
您也可以使用String#lastIndexOf(String str, int fromIndex)
,如下所示:
public class Main {
public static void main(String[] args) {
String input = "Hi Afternoon Hello Afternoon Hi Afternoon GM Afternoon Hi Afternoon";
// Get the index of the last 'Afternoon'
int index1 = input.lastIndexOf("Afternoon");
// Get the index of the second last 'Afternoon'
int index2 = input.lastIndexOf("Afternoon", index1 - 1);
// Create the final string by replacing the second last 'Afternoon' with
// 'GoodNight' and reinstating the remaining substrings as they are
String finalStr = input.substring(0, index2) + "GoodNight" + input.substring(index2 + "Afternoon".length());
System.out.println(finalStr);
}
}
输出:
Hi Afternoon Hello Afternoon Hi Afternoon GM GoodNight Hi Afternoon
首先,查找搜索字符串的最后一个索引。然后获取不包含最后一个搜索字符串的子字符串。
现在找到子字符串中的最后一个索引,那么它将是倒数第二个搜索字符串的起始索引。
然后使用substring方法将搜索字符串替换为新字符串。
String input = "Hi Afternoon Hello Afternoon Hi Afternoon GM Afternoon Hi Afternoon";
String searchString = "Afternoon";
Integer secondLastIndex =
input.substring(0, input.lastIndexOf(searchString)).lastIndexOf(searchString);
input = input.substring(0, secondLastIndex) + "GoodNight"
+ input.substring(secondLastIndex + searchString.length());
输出:
Hi Afternoon Hello Afternoon Hi Afternoon GM GoodNight Hi Afternoon
我确实喜欢这个
void replaceString3(String input) {
int lastIndex = input.lastIndexOf("Afternoon");
String tempString = input.substring(0,input.lastIndexOf("Afternoon"));
//Storing last index
int secondLastIndex = tempString.lastIndexOf("Afternoon");
String replaceString = tempString.substring(secondLastIndex);
// Replace String
String finalReplacedString = replaceString.replace("Afternoon", "GoodNight");
System.out.println("nOutput: "+tempString.substring(0,secondLastIndex)+ finalReplacedString + input.substring(lastIndex));
}