9

I have a string as follows

Hey {1}, you are {2}.

Here 1 and 2 are key whose value will be added dynamically.

Now I need to replace {1} with the value that 1 represents and then I need to replace {2} with the value that 2 represents in the sentence above.

How do I do it?

I know what split function of a string does and I know very well that with that function I can do what I want but I am looking for something better.

Note: I don't know what these keys are in advance. I need to retrieve the keys as well. And then according to the keys I need to replace the values in the string.

Ali
  • 56,466
  • 29
  • 168
  • 265
Smrita
  • 1,259
  • 3
  • 18
  • 38

5 Answers5

13

You can use MessageFormat from java.text.MessageFormat. Message Format has some example on how it can be used in this type of scenario

Taylor
  • 3,942
  • 2
  • 20
  • 33
Pranav Maniar
  • 1,545
  • 10
  • 22
7

Thanks to https://stackoverflow.com/users/548225/anubhava for this one.. :). You could do something like this:

public static void main(String[] args) {
    String s = "Hey {1}, you are {2}.";
    HashMap<Integer, String> hm = new HashMap();
    hm.put(1, "one");
    hm.put(2, "two");
    Pattern p = Pattern.compile("(\\{\\d+\\})");
    Matcher m = p.matcher(s);
    while (m.find()) {
        System.out.println(m.group());
        String val1 = m.group().replace("{", "").replace("}", "");
        System.out.println(val1);
        s = (s.replace(m.group(), hm.get(Integer.parseInt(val1))));
        System.out.println(s);
    }

}

Output:

Hey one, you are two.
Community
  • 1
  • 1
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
4

Try using String.format():

String x = String.format("Hi %s, you are %s\n", str1, str2);

If you have already a string "Hey {1}, you are {2}." you can use regular expressions to replace {1} and {2} with %s.

String template = "Hey {1}, you are {2}.";
String x = String.format(template.replaceAll("\\{\\d\\}", "%s"), "Name", "Surname");
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
4

Try this i think this will work from what i understand from your question

public static void main(String[] args) {
       String str = "Hey {1} your {2} is {3}  clear to {4}  ";
       Map<Integer,String> map = new LinkedHashMap<Integer,String>();
       map.put(1, "Smrita");
       map.put(2, "question");
       map.put(3, "not");
       map.put(4, "anyone");
       while(str.contains("{")){
           Integer i = Integer.parseInt(str.substring(str.indexOf("{")+1, str.indexOf("{")+2));
           str = str.replace("{"+i+"}", map.get(i));

       }
        System.out.println(str);
    }

the output is

Hey Smrita your question is not clear to anyone

amit bhardwaj
  • 883
  • 2
  • 8
  • 18
0

Try this:

String str = "Hey {1}, you are {2}.";
str = str.replaceFirst("\\{.*?\\}", "Matt");

Output:

Hey Matt, you are {2}.

The above code will replace the first instance of {1}. If you call this sequence in a loop replacing with the key values from your code, it'll replace all strings within {...} in the order mentioned in the given string.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
Anoop George
  • 742
  • 6
  • 6