4

So for my app in Android Studio I want to replace the following:

String card = cards.get(count).getCard();
if (card.contains("{Player1}")) {
            String replacedCard = card.replaceAll("{Player1}", "Poep");
}

An example of String card can be: {Player1} switch drinks with the person next to you.

Somehow I can't use {} for the replacing. With the { it says: "Dangling metacharacter". Screenshot: https://prnt.sc/s2bbl8

Is there a solution for this?

1
  • Consider using String.replace instead of String.replaceAll Commented Apr 19, 2020 at 15:25

6 Answers 6

7

the first Argument of replaceAll is a String that is parsed to a regular Expression (regEx). The braces { } are special reserved meta characters to express something within the regular expression. To match them as normal characters, you need to escape them with a leading backslash \ and because the backslash is also a special character you need to escape itself with an additional backslash:

String replacedCard = card.replaceAll("\\{Player1\\}", "Poep");
Sign up to request clarification or add additional context in comments.

Comments

2

Both { } are reserved regex characters. Since the replaceAll() function takes in a regex parameter, you have to explicitly state that { and } are part of your actual string. You can do this by prefixing them with the escape character: \. But because the escape character is also a reserved character, you need to escape it too.

Here's the correct way to write your code:

String card = cards.get(count).getCard();
if (card.contains("{Player1}")) {
    String replacedCard = card.replaceAll("\\{Player1\\}", "Poep");
}

Comments

1

You need to escape the initial { with \. I.e;

        String card = "{Player1}";

        if (card.contains("{Player1}")) {
            String replacedCard = card.replaceAll("\\{Player1}", "Poep");

            System.out.println("replace: " + replacedCard);
        }

Comments

1

The method String.replaceAll expects a regular expression. The other answers already give a solution for this. However, if you don't need regular expressions, then you can also use String.replace:

String replacedCard = card.replace("{Player1}", "Poep");

Comments

1

Since the input value of the replaceAll method expects a regex, you need to escape the curly brackets with a backslash. The curly brackets are special characters in the context of regular expressions.

In Java a backslash in a regex is accomplished by a double backslash \\ (see https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html for reference).

So you would need to adjust the line like so:

String replacedCard = card.replaceAll("\\{Player1\\}", "Poep");

Comments

0

{} are special characters for Regular Expressions. replaceAll method takes as first parameter a Regular Expressions, so if you want also to replace the curly brackets you have to skip them with \\ , as follow:

    String card = cards.get(count).getCard();
    if (card.contains("{Player1}")) {
        String replacedCard = card.replaceAll("\\{Player1}", "Poep");
    } 

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.