本文实例为大家分享了java实现查找替换功能的具体代码,供大家参考,具体内容如下
查找
如果(searchTxt.getText () .equals (" ")) { JOptionPane。showMessageDialog (null,”查找内容不能为空! "); }else if (! searchTxt.getText () .equals (" ")) {//searchDialog.dispose (); 如果(fileChoose。专注==1){ fileChoose.tp1.requestFocus(真正的); str=fileChoose.tp1.getText (); 起动位置=str.indexOf (searchTxt.getText ()); endPosition=起动位置+ searchTxt.getText () . length (); fileChoose.tp1。选择(起动位置,endPosition); newEndPosition=endPosition; 如果(fileChoose}其他。专注==2){ fileChoose.tp2.requestFocus(真正的); str=fileChoose.tp2.getText (); 起动位置=str.indexOf (searchTxt.getText ()); endPosition=起动位置+ searchTxt.getText () . length (); fileChoose.tp2。选择(起动位置,endPosition); newEndPosition=endPosition; } }
查找下一处
nextTemp=newEndPosition; 如果(fileChoose。专注==1){ strAll=fileChoose.tp1.getText (); fileChoose.tp1。选择(nextTemp strAll.length());//选中所有未查找的字符串 nextStr=fileChoose.tp1.getSelectedText (); newStartPosition=nextStr.indexOf (searchTxt.getText ()) + nextTemp;//在未查找的字符串中查找对应字符在tp1中的位置 newEndPosition=newStartPosition + searchTxt.getText () . length (); fileChoose.tp1。选择(newStartPosition newEndPosition);//找到文本,选择文本 如果(fileChoose}其他。专注==2){ strAll=fileChoose.tp2.getText (); fileChoose.tp2。选择(nextTemp strAll.length());//选中所有未查找的字符串 nextStr=fileChoose.tp2.getSelectedText (); newStartPosition=nextStr.indexOf (searchTxt.getText ()) + nextTemp;//在未查找的字符串中查找对应字符在tp1中的位置 newEndPosition=newStartPosition + searchTxt.getText () . length (); fileChoose.tp2。选择(newStartPosition newEndPosition);//找到文本,选择文本 }
替换
如果(fileChoose。专注==1){ fileChoose.tp1.requestFocus(真正的); str=fileChoose.tp1.getText (); 起动位置=str.indexOf (searchTxt.getText ()); endPosition=起动位置+ searchTxt.getText () . length (); fileChoose.tp1.replaceSelection (replaceTxt.getText ()); newEndPosition=endPosition; 如果(fileChoose}其他。专注==2){ fileChoose.tp2.requestFocus(真正的); str=fileChoose.tp2.getText (); 起动位置=str.indexOf (searchTxt.getText ()); endPosition=起动位置+ searchTxt.getText () . length (); fileChoose.tp2.replaceSelection (replaceTxt.getText ()); newEndPosition=endPosition; } >之前替换下一处
如果(fileChoose。专注==1){ fileChoose.tp1.requestFocus(真正的); nextTemp=newEndPosition; strAll=fileChoose.tp1.getText ();////////////////////////////////fileChoose.tp1。选择(nextTemp strAll.length ());/////////////////////////////nextStr=fileChoose.tp1.getSelectedText (); newStartPosition=nextStr.indexOf (searchTxt.getText ()) + nextTemp; newEndPosition=newStartPosition + searchTxt.getText () . length (); fileChoose.tp1。选择(newStartPosition newEndPosition); fileChoose.tp1.replaceSelection (replaceTxt.getText ()); 如果(fileChoose}其他。专注==2){ fileChoose.tp2.requestFocus(真正的); 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。选择(newStartPosition newEndPosition); fileChoose.tp2.replaceSelection (replaceTxt.getText ()); }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
java实现查找替换功能