java realizes the function of finding and replacing

  • 2021-07-13 05:17:56
  • OfStack

In this article, we share the specific code of java to achieve the function of finding and replacing, for your reference, the specific contents are as follows

Find


if(searchTxt.getText().equals("")){
          JOptionPane.showMessageDialog(null, " Find content cannot be empty! ");
        }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;
          }
     }

Find the next 1 place


nextTemp = newEndPosition;
        if(fileChoose.focus == 1){
          strAll = fileChoose.tp1.getText();
          fileChoose.tp1.select(nextTemp, strAll.length());// Select all unsearched strings 
          nextStr = fileChoose.tp1.getSelectedText();
          newStartPosition = nextStr.indexOf(searchTxt.getText()) + nextTemp;// Find the corresponding character in an unfound string in the tp1 Position in 
          newEndPosition = newStartPosition + searchTxt.getText().length();
          fileChoose.tp1.select(newStartPosition, newEndPosition);// Find the text and select the text 
        }else if(fileChoose.focus == 2){
          strAll = fileChoose.tp2.getText();
          fileChoose.tp2.select(nextTemp, strAll.length());// Select all unsearched strings 
          nextStr = fileChoose.tp2.getSelectedText();
          newStartPosition = nextStr.indexOf(searchTxt.getText()) + nextTemp;// Find the corresponding character in an unfound string in the tp1 Position in 
          newEndPosition = newStartPosition + searchTxt.getText().length();
          fileChoose.tp2.select(newStartPosition, newEndPosition);// Find the text and select the text 
    }

Replace


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;
   }

Replace the next 1


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());
    }

Related articles: