从java中的String(段落)中查找单词(字符串)的最后一个索引

时间:2015-08-26 10:13:40

标签: java string

从java?

中的字符串(段落)中查找单词(字符串)的最后一个索引
String sample="Give your club and/or district the publicity and recognition it deserves! Tell your local community about Toastmasters events, programs and member achievements. Use the news templates below to garner interest and awares are formatted and downloadable as Word documents.Click on a news release and save it to your computer. Localize the news release by replacing the bolded text with your club or district information. Proofread it for accuracy. "

String selectedText = tarea.getSelectedText(); // Select some text from the jTextpane then here -> selectedText = about

我需要找到selectedtext的最后一个索引而不是Jtextpane。我想在样本(String)

中找到单词(about)

1 个答案:

答案 0 :(得分:4)

使用String::lastIndexOf()String::contains()

根据JPane::getSelectedText()

  

返回此TextComponent中包含的选定文本。如果选择为null或文档为空,则返回null。

检查是否选择了文本,并总和初始索引selectedText的长度以获取最后一次出现的最后一个字符。

String sample="Give your club and/or district the publicity and recognition it deserves! Tell your local community about Toastmasters events, programs and member achievements. Use the news templates below to garner interest and awares are formatted and downloadable as Word documents.Click on a news release and save it to your computer. Localize the news release by replacing the bolded text with your club or district information. Proofread it for accuracy."
String selectedText = tarea.getSelectedText();
int lastIndex = 0;
int firstIndex = 0;
if (!null.equals(sample) && sample.contains(selectedText)) {
    lastIndex = sample.lastIndexof(selectedText) + selectedText.length;
    firstIndex = sample.lastIndexof(selectedText);
}