服务器之家

服务器之家 > 正文

java实现查找替换功能

时间:2021-07-27 11:35     来源/作者:cat_pp

本文实例为大家分享了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());
    }

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

标签:

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国 2021-05-08
返回顶部