简体   繁体   English

如何在文本区域中搜索单词

[英]How To Search the Word In Textarea

I want to Highlight The word from Caret Position to End of The Textarea then Highlight the word from Starting of the textarea to end then repeat the process in circular manner.My code works for current caret position to end of the textarea only.Please check it once. 我想突出显示从插入位置到文本区域结尾的单词,然后突出显示从开始文本区域到结束的单词,然后以循环方式重复此过程。我的代码仅适用于当前插入位置到文本区域的结尾,请检查一下一旦。

My Code: 我的代码:

public void highLightWholeWord(JTextArea component, String patteren) {
try {
    Document doc = component.getDocument();
    String text = component.getText(0, doc.getLength());
    int pos = component.getCaretPosition();
    boolean found = false;
    int findLength = patteren.length();
    // Rest the search position if we're at the end of the document
    if (pos + findLength > doc.getLength()) {
        pos = 0;
    }
    while (pos + findLength <= doc.getLength()) {
        // Extract the text from teh docuemnt
        String match = doc.getText(pos, findLength).toLowerCase();
        // Check to see if it matches or request
        if (match.equals(patteren)) {
            if (pos - 1 >= 0
                        && Character.isWhitespace(doc.getText(pos - 1, 1).charAt(0))
                                        || Character.isWhitespace(doc.getText(findLength, 1).charAt(0))) {
                if (pos + findLength == doc.getLength()
                                             || Character.isWhitespace(doc.getText(pos + findLength, 1).charAt(0))) {
                    found = true;
                    break;
                }
            }
        }
        pos++;
    }
    if (found) {
    component.setSelectionStart(pos);
    component.setSelectionEnd(pos + patteren.length());
    component.getCaret().setSelectionVisible(true);
    }
} 
catch (Exception e) {
    e.printStackTrace();
}
}

Let's see a text for example, and suppose we are searching for the word "xxx". 让我们看一个例如的文本,并假设我们正在搜索单词“ xxx”。 Suppose the last time you clicked the button it selected the last xxx: 假设最后一次单击按钮时它选择了最后一个xxx:

xxx ttt aaa xxx fff xxx yyy xxx zzz xxx ttt aaa xxx fff xxx yyy xxx zzz

The size of this text is 35 characters. 该文本的大小为35个字符。

Now, the caret position is at the end of the highlighted xxx (position 31). 现在,插入符号位置位于突出显示的xxx的末尾(位置31)。 And the pattern length is 3. You have an if condition that says: 模式长度为3。如果条件为if,则说明:

if (pos + findLength > doc.getLength()) {
    pos = 0;
}

Now, pos + findLength is 31+3 which is 34 . 现在, pos + findLength31+3 ,即34 So it is not greater than doc.getLength() . 因此它大于doc.getLength() So it will not set pos = 0 . 因此它将不会设置pos = 0

So you will start searching for xxx from position 31, but there is no more xxx there. 因此,您将从位置31开始搜索xxx ,但是那里不再有xxx Just zzz . 只是zzz Eventually you'll reach the end of the document and found will not be set. 最终你会到达文件的末尾, found不会设置。 So the selection will not be changed, and the caret will stay in position 31. 因此,选择不会更改,插入号将保持在位置31。

So no matter how many times you press the button, the same thing will happen. 因此,无论您按下按钮多少次,都会发生相同的事情。

This means that your condition for setting pos=0 is wrong. 这意味着您设置pos=0条件是错误的。 It will only work if the xxx is absolutely the last word. 仅当xxx绝对是最后一个字时,它才起作用。

Here is a suggestion to solve this: 这是解决此问题的建议:

Define your condition in another method: 用另一种方法定义条件:

public boolean foundPattern( Document doc, int pos, int findLength  ) {
    String match = doc.getText(pos, findLength).toLowerCase();
    // Check to see if it matches or request
    if (match.equals(patteren)) {
        if (pos - 1 >= 0
                    && Character.isWhitespace(doc.getText(pos - 1, 1).charAt(0))
                                    || Character.isWhitespace(doc.getText(findLength, 1).charAt(0))) {
            if (pos + findLength == doc.getLength()
                                         || Character.isWhitespace(doc.getText(pos + findLength, 1).charAt(0))) {
                return true;
            }
        }
    }
    return false;
}

This is just to make writing the actual while part easier: 这仅仅是为了使编写实际的while部分变得容易:

while (pos + findLength <= doc.getLength()) {

   found = foundPattern( doc, pos, findLength );

   if ( found ) {
      break;
   }

   pos++;

}

if ( ! found ) {
   pos = 0;

   while ( pos + findLength < component.getCaretPosition() ) {
       found = foundPattern( doc, pos, findLength );
       if ( found ) {
          break;
       }

       pos++;
   }
}

So now you have two loops. 所以现在您有两个循环。 If the first doesn't find the pattern (from caret position to end of document), then the second one starts looking from start of document to caret position. 如果第一个未找到模式(从插入号位置到文档末尾),则第二个开始从文档的起始位置到插入号位置查找。

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

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