本文实例为大家分享了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());
} 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持CodeAE代码之家。
|