How to replace "{{customer}}" from this string "Congrats {{customer}}, you become the potential winner of auction" with "xyz".
Thanks in advance, Suggestion appreciated.
like this?
String string = "Congrats {{customer}}";
String newValue = string.replace("{{customer}}", "xyz");
// 'string' remains the same..but 'newValue' has the replacement.
System.out.println(newValue);
Use the replace method on the String object to do this. Since String is immutable it will return a new object instance with the replaced text. Example:
String myString = "Congrats {{customer}}";
myString = myString.replace("{{customer}}","John");
System.out.println(myString);
Output:
Congrats John
See also the String javadoc for many more useful utility methods.
That looks like a mustache template.
See https://github.com/spullara/mustache.java
Typically, in your case:
HashMap<String, Object> scopes = new HashMap<String, Object>();
scopes.put("customer", "xyz");
Writer writer = new OutputStreamWriter(System.out);
MustacheFactory mf = new DefaultMustacheFactory();
Mustache mustache = mf.compile(new StringReader("Congrats {{customer}}, you become the potential winner of auction"), "example");
mustache.execute(writer, scopes);
writer.flush();
Using resources, you can use method like this:
String customer = "Joe Doe";
String textHello;
textHello = String.format(getString(R.string.hello_customer), customer);
where hello_customer is your string resorce strings.xml.
strings.xml part should look like this:
<string name="hello_customer">Congrats %1$s, you become the potential winner of auction with \"xyz\".</string>