服务器之家

服务器之家 > 正文

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());
    }

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

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部