java实现查找替换功能
作者:cat_pp 时间:2021-12-15 00:46:50
本文实例为大家分享了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());
}
来源:https://blog.csdn.net/cat_pp/article/details/50963397
标签:java,查找,替换
0
投稿
猜你喜欢
C#实现类似新浪微博长URL转短地址的方法
2023-06-02 12:59:28
android实现拍照或从相册选取图片
2022-08-08 06:55:36
WPF+Canvas实现平滑笔迹的示例代码
2021-10-06 06:35:13
C#中事件的定义和使用
2022-02-15 13:34:00
mybatisplus中EntityWrapper的常用方法
2022-11-09 21:52:00
SpringBoot配置拦 截器实现过程详解
2023-11-24 17:14:58
Android编程之菜单实现方法
2023-12-21 16:41:24
Java中的Struts2框架拦截 器之实例代码
2023-06-21 19:04:03
windows下java环境变量的设置方法
2022-12-01 03:13:14
java内部类原理与用法详解
2022-12-04 14:41:25
Java中的static关键字深入理解
2022-02-01 12:59:40
Spring boot事件监听实现过程解析
2022-08-29 13:46:02
java 过滤器filter防sql注入的实现代码
2023-08-30 09:56:40
C#获取路由器外网IP,MAC地址的实现代码
2021-07-03 06:01:32
Springboot实现图片上传功能的示例代码
2022-08-05 22:46:44
SpringBoot实现本地存储文件上传及提供HTTP访问服务的方法
2022-09-14 19:09:12
C语言实现扫雷小游戏完整算法详解(附完整代码)
2021-10-30 05:06:58
Java泛型定义与用法实例详解
2023-11-25 11:50:28
java 算法之希尔排序详解及实现代码
2022-07-12 23:09:45
Java实现的3des加密解密工具类示例
2023-08-21 14:00:01