6

Why doesn't the first line replace "(" with an empty string , while the second one does?

 public static void main(String []args){
     String a="This(rab)(bar)";
     a=a.replace("\\(",""); //First
     String b=a.replaceFirst("\\(","");//Second
    System.out.println(a + " "+b);
 }

3 Answers 3

4

There is a difference between replace and replaceFirst. If your IDE shows you the method signatures, you'll see:

enter image description here

See how replace accepts a plain old target whereas replaceFirst accepts a regex?

"\\(" is a regex that means "a single open parenthesis". replace doesn't treat the strings you pass in as regexes. It will simply try to find a backslash followed by an open parenthesis, which does not exist in your string.

If you want to use replace, just use "(".

Sign up to request clarification or add additional context in comments.

6 Comments

So isn't this "(" a regex? It's a string isn't it?
@ErikNouroyan ( is not a valid regex. It doesn’t mean “match an open parenthesis”. \( is valid regex that means “Match an open parenthesis”. In java string literals, the backslash gets escapes to become `\`.
why does replaceFirst take "\\( " as a single paranthesis insted of backslash and single paranthesis?
Because \( is a regex that means “one opening parenthesis” and replaceFirst accepts a regex. ( on its own has special meaning in regex - start of a capturing group, so you need to escape it with `` to mean “one open parenthesis”. @ErikNouroyan
Can you share a valid link to learn more about regexes, please?
|
3

For replace to work you should write:

a=a.replace("(",""); //First

or use replaceAll if you want to pass a regex:

a=a.replaceAll("\\(",""); //First

replace accepts a sequence of characters to replace:

public String replace(CharSequence target, CharSequence replacement)

Therefore, in your case it attempts to replace the 3 characters "\(", not just the single character "(".

2 Comments

In that case why doesn't this work? a=a.replace("(",""); //First
@ErikNouroyan replace does not expect a regex as the first argument. Therefore it tried to replace the actual sub-string "\\(", which is not present in a.
0

The problem is that it is running in replace with several characters and, therefore, what it will look for is \ and (, so that this does not happen the quotation marks should only contain the character to replace :

    a = a.replace("(", ""); // First

Next I leave a sniper with the original proposal and the fixed one :

public class Main {

    private static final Main initRun = new Main();

    public static void main(String[] args) {

        String a = "This(rab)(bar)";

        System.out.println("Original");
        initRun.runOriginal(a);

        System.out.println("Fixed");
        initRun.runFixed(a);

        // Output
        // Original
        // This(rab)(bar)
        // Thisrab)(bar)
        // Fixed
        // Thisrab)bar)
        // Thisrab)bar)
    }

    /**
     * Execute the original proposal
     *
     * @param a String for replace
     */
    void runOriginal(String a) {
        a = a.replace("\\(", ""); // First
        String b = a.replaceFirst("\\(", "");// Second
        System.out.println(a + "\n" + b);
    }

    /**
     * Execute the fixed proposal
     *
     * @param a String for replace
     */
    void runFixed(String a) {

        a = a.replace("(", ""); // First
        String b = a.replaceFirst("\\(", "");// Second
        System.out.println(a + "\n" + b);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.