简体   繁体   中英

Java String replaceAll() method using {} curly brackets

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?

the first Argument of replaceAll is a String that is parsed to a regalar 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");

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

You need to escape the initial { with \. Ie;

        String card = "{Player1}";

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

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

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");

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");

{} 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");
    } 

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