本文實(shí)例為大家分享了java實(shí)現(xiàn)查找替換功能的具體代碼,供大家參考,具體內(nèi)容如下
在陽信等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計 網(wǎng)站設(shè)計制作按需規(guī)劃網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計,營銷型網(wǎng)站,成都外貿(mào)網(wǎng)站建設(shè),陽信網(wǎng)站建設(shè)費(fèi)用合理。
查找
if(searchTxt.getText().equals("")){ JOptionPane.showMessageDialog(null, "查找內(nèi)容不能為空!"); }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;//在未查找的字符串中查找對應(yīng)字符在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;//在未查找的字符串中查找對應(yīng)字符在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()); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。