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());
                }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值