I have pattern like '{YY}-{MM}-{SEQNO}'. I want to replace above string by dynamic value.
For example
{YY} with - 17
{MM} with - 06
{SEQNO} WITH - 0001
What is the way to that in java?
Use String.replace().
String template = "'{YY}-{MM}-{SEQNO}'";
template = template.replace("{YY}", "17");
template = template.replace("{MM}", "06");
template = template.replace("{SEQNO}", "0001");
(Thanks to @RealSkeptic for improvements).
{17}-{06}-{0001}. Also, you probably don't want to replace the template itself.method one(easier method):
String a = "{YY}-{MM}-{SEQNO}";
a = a.replace("YY", "17").replace("MM", "06").replace("SEQNO", "0001");
System.out.println(a);
//Output: {17}-{06}-{0001}
method two:
a = Pattern.compile("YY").matcher(a).replaceAll("17");
a = Pattern.compile("MM").matcher(a).replaceAll("06");
a = Pattern.compile("SEQNO").matcher(a).replaceAll("0001");
System.out.println("My Output is : " +a);
//Output: My Output is : {17}-{06}-{0001}
Method three:
Have a look at this question -without using replace().
String.replaceAll()String.replace()is better, as it doesn't require regular expressions - this is more efficient and has less problems with special characters.