I'm using a String
like:
String message = "%%NAME is inviting you";
I am using message.replaceAll("%%NAME", me);
where me
is a String
. This line of code is not working for me. I was wondering what I was doing wrong?
Code looks more or less OK, though there may be some syntax issues. Here's a working example:
String message = "%%NAME is inviting you.";
String name = "Diana";
String result = message.replaceAll("%%NAME", name);
I would suggest using the format
method instead of replaceAll
in this case.
String template = "%s is inviting you";
String name = "Bob";
String result = String.format(template, name);
String message = "%%name is inviting you";
String uname = "Keyser Sose";
message.replaceAll("%%name", uname);
...will not modify 'message' because Strings (in java) are immutable
String message = "%%name is inviting you";
String uname = "Keyser Sose";
message = message.replaceAll("%%name", uname);
..WILL work. (Note the re-assignment of 'message')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With