Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace '{Name}' in java

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.

like image 491
Akhila Avatar asked Sep 24 '15 06:09

Akhila


People also ask

How do I replace a hyphen in Java?

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", "");

How do you replace a number in Java?

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.

How do you replace a single quote in Java?

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.


1 Answers

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
like image 121
T.J. Crowder Avatar answered Oct 02 '22 17:10

T.J. Crowder