How to create dynamic string in java with the help of Map

public class Util {
public static String str=”your name is ${SD} u r working in ${CN}”;
public static void main(String[] args) {
Map<String, String> map=new HashMap<String, String>();
map.put(“SD”, “Rakesh”);
map.put(“CN”, “Tss Infotech”);
Set<Entry<String, String>> set=map.entrySet();
String string=””;
for (Iterator iterator = set.iterator(); iterator.hasNext();) {
Entry<String, String> entry = (Entry<String, String>) iterator.next();
String key= entry.getKey();
string=str.replace(“${“+key+”}”, entry.getValue());
str=string;
}
System.out.println(string);
}
}

5 thoughts on “How to create dynamic string in java with the help of Map

Leave a Reply

Your email address will not be published. Required fields are marked *