当前位置: 移动技术网 > IT编程>开发语言>Java > java实现查找替换功能

java实现查找替换功能

2019年07月19日  | 移动技术网IT编程  | 我要评论

常用软件运行库,重生之傻妻训夫,广州女装批发市场

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

查找

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

查找下一处

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);//找到文本,选择文本
    }

替换

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

替换下一处

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网