简体   繁体   中英

Java's String .replaceFirst is not working and I don't know if its a bug or I did it wrong

Ok I have a method that is replacing text when I use string.replace() it works but when I switch to relpaceFirst() as shown below it no longer works, what am I doing wrong or missing here?

private void acceptAccButtonActionPerformed(java.awt.event.ActionEvent evt) {      
    int selectedAcTableItem = validAcTable.getSelectedRow();
    int selectedSugTableItem = suggestedAcTable.getSelectedRow();
    if (selectedAcTableItem > 0) {
        String acNameDefthmlText = htmlText;
        String parensName = "";
        String acName = validAcTable.getValueAt(selectedAcTableItem, 0).toString();
        String acDef = validAcTable.getValueAt(selectedAcTableItem, 1).toString();
        String acSent = validAcTable.getValueAt(selectedAcTableItem, 2).toString();
        StringBuilder acBuilder = new StringBuilder(acDef);
        acBuilder.append(" (").append(acName).append(")");            
        if (!acDef.equals("")) {
            parensName = " (" + acName + ")";
            if (htmlText.contains(acName) && !htmlText.contains(acBuilder)){
                String acReplace = acBuilder.toString();
                String acOrigDefName = acDefRow + parensName;
                if (htmlText.contains(acOrigDefName) && parensName.contains(acOrigName)){
                    acNameDefthmlText = htmlText.replaceFirst(acOrigDefName, acReplace);
                } else if (htmlText.contains(acName)) {
                    acNameDefthmlText = htmlText.replaceFirst(acName, acReplace);
                }
                htmlText = acNameDefthmlText;

            }                
            validAcTable.setValueAt(true, selectedAcTableItem, 2);
            Acronym acronym = createNewAcronym(acName, acSent, acDef, true);
            try {

                AcronymDefinitionController.sharedInstance().writeAcronymToExcelSheet(acName, acDef);
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            } catch (InvalidFormatException ex) {
                Exceptions.printStackTrace(ex);
            }
            if (validAcTable.getRowCount() - 1 >= validAcTable.getSelectedRow() + 1) {
                validAcTable.changeSelection(selectedAcTableItem + 1, 0, true, true);
            }
            validAcTable.repaint();
        }
    }

If you notice the signature of two methods in question:

replace(char oldChar,char newChar);
replace(CharSequence target, CharSequence replacement);

replaceFirst(String regex, String replacement);

As you can see, in replaceFirst you matching argument is treated as regex (regular expression), which will cause the difference if any special chars are involved in the argument.

For example: consider below:

System.out.println("abcdab".replace("ab", "ef"));  //<- replaces all
System.out.println("abcdab".replaceFirst("ab", "ef"));//<-replaces first
System.out.println("\\abcdab".replace("\\ab", "ef")); //<-replaces first
System.out.println("\\abcdab".replaceFirst("\\ab", "ef"));
//^ doesn't replace as `\` is an special char

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM