[Q#7577543][A#7577573] How to replace a word in a String?
https://stackoverflow.com/q/7577543
I'm using a String like: 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?
Answer
https://stackoverflow.com/a/7577573
Code looks more or less OK, though there may be some syntax issues. Here's a working example:
APIzation
String message = "%%NAME is inviting you.";
String name = "Diana";
String result = message.replaceAll("%%NAME", name);
package com.stackoverflow.api;
public class Human7577573 {
public static String replaceWordInString(
String message,
String target,
String name
) {
return message.replaceAll(target, name);
}
}
package com.stackoverflow.api;
/**
* How to replace a word in a String?
*
* @author APIzator
* @see <a href="https://stackoverflow.com/a/7577573">https://stackoverflow.com/a/7577573</a>
*/
public class APIzator7577573 {
public static String replaceWord(String message, String name)
throws Exception {
return message.replaceAll("%%NAME", name);
}
}