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) .....