简体   繁体   English

如何从字符串的最后一个逗号中获取最后一个单词

[英]How to get last word from the last comma in a string

I want to retrieve last word before the last semi-colon in a sentence, please see the following. 我想检索句子中最后一个分号之前的最后一个词,请参阅以下内容。

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");
}

I am getting strange Results as 我越来越奇怪的结果

a; last word

in my case, for the above string, i should get wedaweda as last before the last semi-colon. 就我而言,对于上面的字符串,我应该在最后一个分号之前获得wedaweda作为最后一个。

To split a string you'd do the following. 要分割字符串,请执行以下操作。 This would return an array with the elements split on the "separator" 这将返回一个数组,其中元素在“分隔符”上分割

string_object.split("separator")

In your case you'd do 就你而言

met.split(";")

And it would return an array with each part as an element. 并且它将返回一个数组,其中每个部分都作为元素。 Select the last element to get what you need. 选择最后一个元素以获取所需的内容。

Actually. 其实。 You said "wedaweda" should be the last result...? 您说“ wedaweda”应该是最后的结果...? So I assume you mean the last word BEFORE the last semi-colon. 因此,我认为您的意思是最后一个分号之前的最后一个单词。

So do do this you'd do the split as previously stated, and then, you'd get the second to last element in the array 因此,您可以按照前面所述进行拆分,然后,获取数组中倒数第二个元素

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

I tested this code in my IDE and it works exactly as you want. 我在IDE中测试了此代码,它完全可以按照您的要求工作。

That character is a semi-colon (not a comma) and the call to lastIndex() gives you the end of your match, you need a second call to lastIndex() to get the start of your match. 该字符是分号(不是逗号),对lastIndex()的调用使您结束比赛,您需要再次对lastIndex()进行调用才能开始比赛。 Something like, 就像是,

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");
}

Output is 输出是

string having second wedaweda

To then get the last word, you could split on \\\\s+ (a regular expression matching one, or more, white-space characters) like 为了得到最后一个单词,您可以在\\\\s+ (与一个或多个空格字符匹配的正则表达式)上进行拆分,例如

if (laststringa != null) {
    String[] arr = laststringa.split("\\s+");
    System.out.println(arr[arr.length - 1]);
} else {
    System.out.println("No Words");
}

Which outputs (the requested) 哪些输出(请求的)

wedaweda

It must be: 一定是:

String laststringa = met.substring(met.lastIndexOf(";")+1);        
                                                       ^

The Word after means you have to add one to the Position of the last comma. 后面的单词表示您必须在最后一个逗号的位置添加一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM