gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:05:51 24 4
gpt4 key购买 nike

在 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 中找到。我想在 sample(String) 中找到单词(about)

最佳答案

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

根据JPane::getSelectedText()

Returns the selected text contained in this TextComponent. If the selection is null or the document empty, returns 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);
}

关于java - 从java中的字符串(段落)中查找单词(字符串)的最后一个索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32223811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com