资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java实现查找替换功能

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


网站名称:java实现查找替换功能
当前链接:http://cdkjz.cn/article/jcghhj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220