服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java实现查找替换功能

java实现查找替换功能

2021-07-27 11:35cat_pp Java教程

这篇文章主要为大家详细介绍了java实现查找替换功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现查找替换功能的具体代码,供大家参考,具体内容如下

查找

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if(searchtxt.gettext().equals("")){
          joptionpane.showmessagedialog(null, "查找内容不能为空!");
        }else if(!searchtxt.gettext().equals("")){
          //searchdialog.dispose();
          if(filechoose.focus == 1){
            filechoose.tp1.requestfocus(true);
            str = filechoose.tp1.gettext();
            startposition = str.indexof(searchtxt.gettext());
            endposition = startposition + searchtxt.gettext().length();
            filechoose.tp1.select(startposition, endposition);
            newendposition = endposition;
          }else if(filechoose.focus == 2){
            filechoose.tp2.requestfocus(true);
            str = filechoose.tp2.gettext();
            startposition = str.indexof(searchtxt.gettext());
            endposition = startposition + searchtxt.gettext().length();
            filechoose.tp2.select(startposition, endposition);
            newendposition = endposition;
          }
     }

查找下一处

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
nexttemp = newendposition;
        if(filechoose.focus == 1){
          strall = filechoose.tp1.gettext();
          filechoose.tp1.select(nexttemp, strall.length());//选中所有未查找的字符串
          nextstr = filechoose.tp1.getselectedtext();
          newstartposition = nextstr.indexof(searchtxt.gettext()) + nexttemp;//在未查找的字符串中查找对应字符在tp1中的位置
          newendposition = newstartposition + searchtxt.gettext().length();
          filechoose.tp1.select(newstartposition, newendposition);//找到文本,选择文本
        }else if(filechoose.focus == 2){
          strall = filechoose.tp2.gettext();
          filechoose.tp2.select(nexttemp, strall.length());//选中所有未查找的字符串
          nextstr = filechoose.tp2.getselectedtext();
          newstartposition = nextstr.indexof(searchtxt.gettext()) + nexttemp;//在未查找的字符串中查找对应字符在tp1中的位置
          newendposition = newstartposition + searchtxt.gettext().length();
          filechoose.tp2.select(newstartposition, newendposition);//找到文本,选择文本
    }

替换

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if(filechoose.focus == 1){
          filechoose.tp1.requestfocus(true);
          str = filechoose.tp1.gettext();
          startposition = str.indexof(searchtxt.gettext());
          endposition = startposition + searchtxt.gettext().length();
          filechoose.tp1.replaceselection(replacetxt.gettext());
          newendposition = endposition;
        }else if(filechoose.focus == 2){
          filechoose.tp2.requestfocus(true);
          str = filechoose.tp2.gettext();
          startposition = str.indexof(searchtxt.gettext());
          endposition = startposition + searchtxt.gettext().length();
          filechoose.tp2.replaceselection(replacetxt.gettext());
          newendposition = endposition;
   }

替换下一处

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if(filechoose.focus == 1){
          filechoose.tp1.requestfocus(true);
          nexttemp = newendposition;
          strall = filechoose.tp1.gettext();////////////////////////////////
          filechoose.tp1.select(nexttemp, strall.length());/////////////////////////////
          nextstr = filechoose.tp1.getselectedtext();
          newstartposition = nextstr.indexof(searchtxt.gettext()) + nexttemp;
          newendposition = newstartposition + searchtxt.gettext().length();
          filechoose.tp1.select(newstartposition, newendposition);
          filechoose.tp1.replaceselection(replacetxt.gettext());
        }else if(filechoose.focus == 2){
          filechoose.tp2.requestfocus(true);
          nexttemp = newendposition;
          strall = filechoose.tp2.gettext();
          filechoose.tp2.select(nexttemp,strall.length());
          nextstr = filechoose.tp2.getselectedtext();
          newstartposition = nextstr.indexof(searchtxt.gettext()) + nexttemp;
          newendposition =newstartposition + searchtxt.gettext().length();
          filechoose.tp2.select(newstartposition, newendposition);
          filechoose.tp2.replaceselection(replacetxt.gettext());
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐