时间:2021-05-19
本文实例为大家分享了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()); }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
支持linux正则表达式的工具有:grep:实现查找,sed,awk:都是流式编辑器,可以实现查找和替换,并且把替换的文本输出到屏幕上。grep工具grep[-
本文实例讲述了Java基于正则表达式实现查找匹配的文本功能。分享给大家供大家参考,具体如下:REMatch.java:packagereMatch;import
经常要使用到Linux的批量查找与替换,这里我们为大家介绍使用sed命令来实现查找文件中的内容并替换。语法格式sed-i"s/原字符串/新字符串/g"`grep
本文实例讲述了Python实现查找匹配项作处理后再替换回去的方法。分享给大家供大家参考,具体如下:这里实现Python在对找到的匹配项进行适当处理后,再替换掉原
Excel需要批量删除内容,可通过查找替换的“替换”功能实现。 具体方法步骤如下: 1、打开需要操作的EXCEL表格,在开始选项卡中找到并点击“查找和替换”