Reputation: 377
How to replace "{{customer}}" from this string "Congrats {{customer}}, you become the potential winner of auction" with "xyz".
Thanks in advance, Suggestion appreciated.
Upvotes: 2
Views: 5069
Reputation: 363
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>
Upvotes: 0
Reputation: 39386
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();
Upvotes: 0
Reputation: 13647
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);
Upvotes: 3
Reputation: 3823
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.
Upvotes: 1