0

I have a string like:

{abc} say hello to {def};

or

You say hello to {abc};

Now I want to replace this sentence like this:

Peter say hello to Sally;

or

You say hello to Peter

which {abc} and {def} are user's ID, so I need to get the string inside { }. Then replace these words. How can I get the words inside { }

I tried to use .indexOf and .substring to make it work. But I think it is not a good idea as it need a lot of codes to handle two kinds. My method:

String sentence = {abc} say hello to {def};
int firstOpen = sentence.indexOf("{");
int firstClose = sentence.indexOf("}");
int secondOpen = sentence.lastIndexOf("{");
int secondClose = sentence.lastIndexOf("}");
String firstName = sentence.subSting(firstOpen + 1, firstClose);
String secondName = sentence.subSting(secondOpen + 1, secondClose);
...
sentence = "Peter" + sentence.substring(firstClose, secondOpen) .....
Yunyuen Chan
  • 135
  • 1
  • 11
  • Possible duplicate of [Named placeholders in string formatting](http://stackoverflow.com/questions/2286648/named-placeholders-in-string-formatting) – Robby Cornelissen Mar 29 '17 at 04:11

3 Answers3

1

Just use String.replace

String output = "{abc} say hello to {def}".replace ("{abc}", "Peter")
                                          .replace ("{def}", "Sally");
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

If you're not sure what word would be inside curly braces, then you can use these regex to make them generic

input = input.replaceFirst("^\\{([^}]*)\\}", "Peter").replaceFirst("\\{([^}]*)\\};$", "Sally");

Output:

Peter say hello to Sally

You can use this program if you want to save the group ids.

Output

first Id: abc                                                                                                                                                                                              
second Id: def                                                                                                                                                                                             
replaced String: Peter say hello to Sally

Code #2

import java.util.regex.*;

public class HelloWorld {

    public static void main(String[] args) {
        String line = "{abc} say hello to {def};";

        Matcher m = Pattern.compile("^\\{([^}]*)\\}(.*)\\{([^}]*)\\};$").matcher(line);
        if (m.find()) {
            System.out.println("first Id: " + m.group(1));
            System.out.println("second Id: " + m.group(3));
            System.out.println("replaced String: " + "Peter" + m.group(2) + "Sally");
        }
    }
}
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
-1

You can use the String.replaceFirst method

String sentence = "{abc} say hello to {def}";
String firstChange = sentence.replaceFirst("\\{abc\\}","you");
String secondChange = firstChagne.replaceFirst("\\{def\\}","Peter");
1283822
  • 1,832
  • 17
  • 13
  • `replaceFirst` takes a regex, and `{` and `}` are special characters. This will produce an error unless you escape those characters (i.e. `"\\{abc\\}"`) – 4castle Mar 29 '17 at 04:09
  • Sorry. I miss something from the question. I also need to get the words inside "{ }" – Yunyuen Chan Mar 29 '17 at 04:16