java實(shí)現(xiàn)查找替換功能
本文實(shí)例為大家分享了java實(shí)現(xiàn)查找替換功能的具體代碼,供大家參考,具體內(nè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;//在未查找的字符串中查找對(duì)應(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;//在未查找的字符串中查找對(duì)應(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()); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java枚舉使用詳細(xì)介紹及實(shí)現(xiàn)
這篇文章主要介紹了java枚舉使用詳細(xì)介紹及實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-06-06SpringBoot注冊(cè)Filter的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了SpringBoot注冊(cè)Filter的兩種實(shí)現(xiàn)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Java中HTTP GET方法調(diào)用帶有body的問題解決
這篇文章主要為大家詳細(xì)介紹了Java如何解決HTTP GET方法調(diào)用帶有body的問題,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2024-02-02java程序員必會(huì)的遠(yuǎn)程debug教程
這篇文章主要為大家介紹了java程序員必會(huì)的遠(yuǎn)程debug教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Spring集成JPA配置懶加載報(bào)錯(cuò)解決方案
這篇文章主要介紹了Spring集成JPA配置懶加載報(bào)錯(cuò)解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10Mybatis批量修改聯(lián)合主鍵數(shù)據(jù)的兩種方法
最近遇上需要批量修改有聯(lián)合主鍵的表數(shù)據(jù),找很多資料都不是太合適,最終自己摸索總結(jié)了兩種方式可以批量修改數(shù)據(jù),對(duì)Mybatis批量修改數(shù)據(jù)相關(guān)知識(shí)感興趣的朋友一起看看吧2022-04-04Java使用junit框架進(jìn)行代碼測(cè)試過(guò)程詳解
單元測(cè)試就是針對(duì)最小的功能單元編寫測(cè)試代碼,Junit是使用Java語(yǔ)言實(shí)現(xiàn)的單元測(cè)試框架,它是開源的,Java開發(fā)者都應(yīng)當(dāng)學(xué)習(xí)并使用Junit編寫單元測(cè)試。本文就來(lái)講講Junit框架的使用教程,需要的可以參考一下2023-02-02mybatis的mapper特殊字符轉(zhuǎn)移及動(dòng)態(tài)SQL條件查詢小結(jié)
mybatis mapper文件中條件查詢符,如>=,<,之類是不能直接寫的會(huì)報(bào)錯(cuò)的需要轉(zhuǎn)移一下,本文給大家介紹了常見的條件查詢操作,對(duì)mybatis的mapper特殊字符及動(dòng)態(tài)SQL條件查詢相關(guān)知識(shí)感興趣的朋友一起看看吧2021-09-09