在Java中查找字符串中最后一秒出现的单词
我必须使用string方法找出单词在字符串中的第二个最后出现的位置:在Java中查找字符串中最后一秒出现的单词,java,string,Java,String,我必须使用string方法找出单词在字符串中的第二个最后出现的位置: String input=“你好下午你好下午你好下午GM下午你好下午” 输出应该是 Output = Hi Afternoon Hello Afternoon Hi Afternoon GM GoodNight Hi Afternoon 你能帮我写下面的代码吗?不知怎的,它用晚安代替了整个下午: System.out.println(input.lastIndexOf("Afternoon"));
String input=“你好下午你好下午你好下午GM下午你好下午”代码>
输出应该是
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"));
首先,找到搜索字符串的最后一个索引。然后获取没有最后一个搜索字符串的子字符串
现在在子字符串中找到最后一个索引,然后它将是第二个最后一个搜索字符串开始索引
然后使用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
首先,找到搜索字符串的最后一个索引。然后获取没有最后一个搜索字符串的子字符串
现在在子字符串中找到最后一个索引,然后它将是第二个最后一个搜索字符串开始索引
然后使用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
按如下方式操作:
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
Hi Afternoon Hello Afternoon Hi Afternoon GM GoodNight Hi Afternoon
[更新]
您也可以按如下所示使用:
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
Hi Afternoon Hello Afternoon Hi Afternoon GM GoodNight Hi Afternoon
按如下方式操作:
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
Hi Afternoon Hello Afternoon Hi Afternoon GM GoodNight Hi Afternoon
[更新]
您也可以按如下所示使用:
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
Hi Afternoon Hello Afternoon Hi Afternoon GM GoodNight Hi Afternoon
要查找第二个索引,请先调用,然后调用,传入第一个调用的值
现在可以使用和生成结果
静态字符串替换secondlast(字符串输入、字符串搜索、字符串替换){
int lastIdx=input.lastIndexOf(搜索);
int secondLastIdx=(lastIdx查找第二个索引,first call,然后call,传入第一个调用的值
现在可以使用和生成结果
静态字符串替换secondlast(字符串输入、字符串搜索、字符串替换){
int lastIdx=input.lastIndexOf(搜索);
int secondLastIdx=(lastIdx一种方法是使用显示的子字符串和其他答案提供的子字符串
如果您想在边缘生活,可以使用regex replace,利用一行代码的反向引用:
input.replaceFirst((.*)(下午)(.*)(?=(\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
一种方法是使用显示的子字符串和其他答案提供的子字符串
如果您想在边缘生活,可以使用regex replace,利用一行代码的反向引用:
input.replaceFirst((.*)(下午)(.*)(?=(\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
我确实喜欢这个
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));
}
我确实喜欢这个
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));
}
不必要的自动装箱:当int
可以使用时,不要使用Integer
。--lastIndex
变量的用途是什么?它从未使用过。--当存在另一个lastIndexOf()时,使用substring()
查找第二个最后一个索引是次优的
方法,运行速度更快。--如果搜索字符串两次都找不到,代码就会崩溃。@Andreas谢谢你的建议!@Andreas好的,然后我回到我最初的解决方案,也许我误解了你的评论不,你没有。我只是觉得有趣的是,你的解决方案最终与我的完全相同。我对更改没有问题e、 这就是我添加wink表情符号的原因。不必要的自动装箱:当int
可以使用时,不要使用Integer
。-lastIndex
变量的用途是什么?它从未被使用过。--当存在另一个lastIndexOf()时,使用substring()
查找倒数第二个索引是次优的
方法,运行速度更快。--如果搜索字符串两次都找不到,代码就会崩溃。@Andreas谢谢你的建议!@Andreas好的,然后我回到我最初的解决方案,也许我误解了你的评论不,你没有。我只是觉得有趣的是,你的解决方案最终与我的完全相同。我对更改没有问题e、 这就是我添加wink表情符号的原因。使用substring()当存在另一个lastIndexOf()时,查找倒数第二个索引是次优的
工作速度更快的方法。--如果搜索字符串找不到两次,代码就会崩溃。这种事情需要以逻辑的方式考虑。此外,使用Java实现这一目的,至少在开始步骤中是没有意义的。您需要从文本末尾开始扫描想要的单词,找到想要的单词的第一个匹配项,保持其索引,然后再次从该索引开始扫描。这类事情需要以逻辑方式考虑。此外,为此目的使用Java(至少在开始步骤中)是不合理的。您需要从文本末尾开始扫描所需单词,找到所需单词的第一个匹配项,保持其索引,然后再次启动sca从该索引中删除。被否决,因为您的答案实际上与其他人提供的答案没有什么不同(我的答案包含在替换中,但您的答案将替换所有出现的内容,而不仅仅是您想要的内容),此外,正如Andreas指出的,当字符串中没有找到2个实例时,它将失败。它将抛出IndexOutOfBoundsException
否决票,因为您的答案实际上与其他人提供的答案没有什么不同(我的包含在replace
中,除了你的将替换所有出现的事件,而不仅仅是你想要的),此外,正如Andreas指出的,当字符串中没有找到2个实例时,它将失败。它将抛出IndexOutOfBoundsException