I need to replace a value in a string like {Name}
with values.
How can we replace the special character {
and }
?
I have tried this:
str.replaceAll("{Name}","A");
But this doesnt work if we have special characters.
String is Immutable in Java. You have to reassign it to get the result back: String str ="your string with dashesh"; str= str. replace("\u2014", "");
you can use Java - String replaceAll() Method. This method replaces each substring of this string that matches the given regular expression with the given replacement. Here is the detail of parameters: regex -- the regular expression to which this string is to be matched.
This uses String. replace(CharSequence, CharSequence) method to do string replacement. Remember that \ is an escape character for Java string literals; that is, "\\'" contains 2 characters, a backslash and a single quote.
Use replace
rather than replaceAll
, since replace
doesn't expect and parse a regular expression.
Example: (live copy)
String str = "Here it is: {Name} And again: {Name}";
System.out.println("Before: " + str);
str = str.replace("{Name}","A");
System.out.println("After: " + str);
Output:
Before: Here it is: {Name} And again: {Name} After: Here it is: A And again: A
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