java - How to use String replace correctly in this case? -
i have got sms template shown below
user: xxxxxxxxxxxxxxxxxx, ph: xxxxxxxxxx, loc: xxxxxxxxxxxxxxxxxxxx, area: xxxxxxxxxxxxxxxxxxxx, added home delivery. please review details
where xxxxxx needs filled dynamic values
i have tried way shown below
but when run program output getting
user: kiran, ph: 11111111111111, loc: kiranxx, area: kiranxx, added home delivery. please review details
it adding xx end program
import java.net.unknownhostexception; import org.apache.log4j.logger; public class test { final static logger logger = logger.getlogger(test.class); public static void main(string args[]) throws unknownhostexception { string user = "kiran"; string phone = "11111111111111"; string loc = "my location"; string area = "my area"; string message = "user: xxxxxxxxxxxxxxxxxx, ph: xxxxxxxxxx, loc: xxxxxxxxxxxxxxxxxxxx, area: xxxxxxxxxxxxxxxxxxxx, added home delivery. please review details\r\n" + ""; message = message.replace("xxxxxxxxxxxxxxxxxx", user); message = message.replace("xxxxxxxxxx", phone); message = message.replace("xxxxxxxxxxxxxxxxxxxx", loc); message = message.replace("xxxxxxxxxxxxxxxxxxxx", area); system.out.println(message); } }
could please tell me how resolve issue ??
you should use this:
string template = "user: %s, ph: %s, loc: %s, area: %s, added home delivery. please review details\r\n"; string result = string.format(template, user, phone, loc, area);
Comments
Post a Comment