Java 如何从字符串中的最后一个逗号获取最后一个单词
我想检索句子中最后一个分号之前的最后一个单词,请参见下面的内容Java 如何从字符串中的最后一个逗号获取最后一个单词,java,android,Java,Android,我想检索句子中最后一个分号之前的最后一个单词,请参见下面的内容 String met = "This is the string with comma numberone; string having second wedaweda; last word"; String laststringa = met.substring(met.lastIndexOf(";")-1); if(laststringa != null) { System.out.println(laststringa
String met = "This is the string with comma numberone; string having second wedaweda; last word";
String laststringa = met.substring(met.lastIndexOf(";")-1);
if(laststringa != null)
{
System.out.println(laststringa);
}else{
System.out.println("No Words");
}
我也得到了奇怪的结果
a; last word
在我的情况下,对于上面的字符串,我应该
韦达韦达
最后一个分号之前的最后一个分号 必须是:
String laststringa = met.substring(met.lastIndexOf(";")+1);
^
后面的单词表示必须在最后一个逗号的位置添加一个逗号。必须是:
String laststringa = met.substring(met.lastIndexOf(";")+1);
^
后面的单词表示必须在最后一个逗号的位置添加一个逗号。必须是:
String laststringa = met.substring(met.lastIndexOf(";")+1);
^
后面的单词表示必须在最后一个逗号的位置添加一个逗号。必须是:
String laststringa = met.substring(met.lastIndexOf(";")+1);
^
后面的单词表示必须在最后一个逗号的位置添加一个逗号。要拆分字符串,请执行以下操作。这将返回一个在“分隔符”上拆分元素的数组 对你来说,你会的
met.split(";")
它将返回一个数组,每个部分作为一个元素。选择最后一个元素以获取所需内容
事实上。你说“wedaweda”应该是最后的结果。。。?我想你是指最后一个分号之前的最后一个词
所以这样做你会像前面所说的那样进行分割,然后,你会得到数组中倒数第二个元素
String[] array = met.split(";'); // split the entire first sentence on semi-colons
String[] words = array[array.length - 2] .split(" "); // split into specific words by splitting on a blank space
String wordBeforeSemiColon = words[words.length - 1]; // get the word directly before the last semi-colon
我在我的IDE中测试了这段代码,它的工作方式完全符合您的要求。要拆分字符串,请执行以下操作。这将返回一个在“分隔符”上拆分元素的数组 对你来说,你会的
met.split(";")
它将返回一个数组,每个部分作为一个元素。选择最后一个元素以获取所需内容
事实上。你说“wedaweda”应该是最后的结果。。。?我想你是指最后一个分号之前的最后一个词
所以这样做你会像前面所说的那样进行分割,然后,你会得到数组中倒数第二个元素
String[] array = met.split(";'); // split the entire first sentence on semi-colons
String[] words = array[array.length - 2] .split(" "); // split into specific words by splitting on a blank space
String wordBeforeSemiColon = words[words.length - 1]; // get the word directly before the last semi-colon
我在我的IDE中测试了这段代码,它的工作方式完全符合您的要求。要拆分字符串,请执行以下操作。这将返回一个在“分隔符”上拆分元素的数组 对你来说,你会的
met.split(";")
它将返回一个数组,每个部分作为一个元素。选择最后一个元素以获取所需内容
事实上。你说“wedaweda”应该是最后的结果。。。?我想你是指最后一个分号之前的最后一个词
所以这样做你会像前面所说的那样进行分割,然后,你会得到数组中倒数第二个元素
String[] array = met.split(";'); // split the entire first sentence on semi-colons
String[] words = array[array.length - 2] .split(" "); // split into specific words by splitting on a blank space
String wordBeforeSemiColon = words[words.length - 1]; // get the word directly before the last semi-colon
我在我的IDE中测试了这段代码,它的工作方式完全符合您的要求。要拆分字符串,请执行以下操作。这将返回一个在“分隔符”上拆分元素的数组 对你来说,你会的
met.split(";")
它将返回一个数组,每个部分作为一个元素。选择最后一个元素以获取所需内容
事实上。你说“wedaweda”应该是最后的结果。。。?我想你是指最后一个分号之前的最后一个词
所以这样做你会像前面所说的那样进行分割,然后,你会得到数组中倒数第二个元素
String[] array = met.split(";'); // split the entire first sentence on semi-colons
String[] words = array[array.length - 2] .split(" "); // split into specific words by splitting on a blank space
String wordBeforeSemiColon = words[words.length - 1]; // get the word directly before the last semi-colon
我在我的IDE中测试了这段代码,它的工作方式完全符合您的需要。该字符是分号(不是逗号),调用
lastIndex()
可以结束匹配,您需要再次调用lastIndex()
来获得匹配的开始。大概
String met = "This is the string with comma numberone; string having second wedaweda; last word";
int lastIndex = met.lastIndexOf(";");
int prevIndex = met.lastIndexOf(";", lastIndex - 1);
String laststringa = met.substring(prevIndex + 1, lastIndex).trim();
if (laststringa != null) {
System.out.println(laststringa);
} else {
System.out.println("No Words");
}
输出为
string having second wedaweda
为了得到最后一个单词,您可以使用\\s+
(与一个或多个空格字符匹配的正则表达式)进行拆分,如
哪些输出(请求的)
该字符是分号(不是逗号),对
lastIndex()
的调用给出了匹配的结束,您需要第二次调用lastIndex()
来获得匹配的开始。大概
String met = "This is the string with comma numberone; string having second wedaweda; last word";
int lastIndex = met.lastIndexOf(";");
int prevIndex = met.lastIndexOf(";", lastIndex - 1);
String laststringa = met.substring(prevIndex + 1, lastIndex).trim();
if (laststringa != null) {
System.out.println(laststringa);
} else {
System.out.println("No Words");
}
输出为
string having second wedaweda
为了得到最后一个单词,您可以使用\\s+
(与一个或多个空格字符匹配的正则表达式)进行拆分,如
哪些输出(请求的)
该字符是分号(不是逗号),对
lastIndex()
的调用给出了匹配的结束,您需要第二次调用lastIndex()
来获得匹配的开始。大概
String met = "This is the string with comma numberone; string having second wedaweda; last word";
int lastIndex = met.lastIndexOf(";");
int prevIndex = met.lastIndexOf(";", lastIndex - 1);
String laststringa = met.substring(prevIndex + 1, lastIndex).trim();
if (laststringa != null) {
System.out.println(laststringa);
} else {
System.out.println("No Words");
}
输出为
string having second wedaweda
为了得到最后一个单词,您可以使用\\s+
(与一个或多个空格字符匹配的正则表达式)进行拆分,如
哪些输出(请求的)
该字符是分号(不是逗号),对
lastIndex()
的调用给出了匹配的结束,您需要第二次调用lastIndex()
来获得匹配的开始。大概
String met = "This is the string with comma numberone; string having second wedaweda; last word";
int lastIndex = met.lastIndexOf(";");
int prevIndex = met.lastIndexOf(";", lastIndex - 1);
String laststringa = met.substring(prevIndex + 1, lastIndex).trim();
if (laststringa != null) {
System.out.println(laststringa);
} else {
System.out.println("No Words");
}
输出为
string having second wedaweda
为了得到最后一个单词,您可以使用\\s+
(与一个或多个空格字符匹配的正则表达式)进行拆分,如
哪些输出(请求的)
这似乎更容易的选项这似乎更容易的选项这似乎更容易的选项很多,这是我想要的谢谢很多,这是我想要的谢谢很多,这是我想要的谢谢很多,这是我想要的谢谢很多,这是我想要的