java实现查找替换功能

所属分类: 软件编程 / java 阅读数: 32
收藏 0 赞 0 分享

本文实例为大家分享了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 Set简介_动力节点Java学院整理

Set最大的特性就是不允许在其中存放的元素是重复的。接下来通过本文给大家分享java set常用方法和原理分析,需要的的朋友参考下吧
收藏 0 赞 0 分享

Java Timezone类常见问题_动力节点Java学院整理

这篇文章主要介绍了Java Timezone类常见问题的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

javaWeb项目部署到阿里云服务器步骤详解

本篇文章主要介绍了javaWeb项目部署到阿里云服务器步骤详解,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解使用zxing库生成QR-Code二维码

这篇文章主要介绍了详解使用zxing库生成QR-Code二维码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现对服务器的自动巡检邮件通知

这篇文章主要为大家详细介绍了java实现对服务器的自动巡检邮件通知,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java随机验证码生成实现实例代码

这篇文章主要介绍了java随机验证码生成实现实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

java读取txt文件代码片段

这篇文章主要为大家详细介绍了java读取txt文件的代码片段,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java连接mysql数据库的方法

这篇文章主要为大家详细介绍了java连接mysql数据库的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java 算法之快速排序实现代码

这篇文章主要介绍了java 算法之快速排序实现代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用

这篇文章主要介绍了详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多