在textarea中搜索单词 [英] Searching for words in textarea

查看:153
本文介绍了在textarea中搜索单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java中建立一个自定义的查找并替换。我浏览一个文本文件并在textarea中加载内容。现在我有一个文本框,在那里输入需要搜索的文本。



搜索文本的最佳方式是什么?我知道使用 string.indexOf()的方法,但我也需要突出显示。所以请帮助我。

首先阅读文本和新行获取有关如何获取要搜索的文本的信息。



然后突出显示您需要使用荧光笔的文字。代码如下所示:

  Highlighter.HighlightPainter painter = $ b $ new DefaultHighlighter.DefaultHighlightPainter(Color.cyan); 

int offset = text.indexOf(searchWord);
int length = searchWord.length();

while(offset!= -1)
{
try
{
textPane.getHighlighter()。addHighlight(offset,offset + length,painter );
offset = text.indexOf(searchWord,offset + 1);
}
catch(BadLocationException ble){System.out.println(ble); }
}


I am building a custom a find and replace in java. I browse a text file and load the contents in a textarea. Now I have a textBox, where I input a text that needs to be searched.

What is the best way to search the text. I know a way using string.indexOf(), but I also need highlighting. So please help me out.

解决方案

First of all read Text and New Lines for information on how to get the text to search.

Then to highlight the text your find you need to use a Highlighter. The code is something like:

Highlighter.HighlightPainter painter = 
    new DefaultHighlighter.DefaultHighlightPainter( Color.cyan );

int offset = text.indexOf(searchWord);
int length = searchWord.length();

while ( offset != -1)
{
    try
    {
        textPane.getHighlighter().addHighlight(offset, offset + length, painter);
        offset = text.indexOf(searchWord, offset+1);
    }
    catch(BadLocationException ble) { System.out.println(ble); }
}

这篇关于在textarea中搜索单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆