user2778018
user2778018

Reputation: 89

Remove curly brace in Java

I have a text file in which each line begins and ends with a curly brace:

{aaa,":"bbb,ID":"ccc,}  
{zzz,":"sss,ID":"fff,}  
{ggg,":"hhh,ID":"kkk,} ...  

Between the characters there are no spaces. I'm trying to remove the curly braces and replace them with white space as follows:

String s = "{aaa,":"bbb,ID":"ccc,}";
String n = s.replaceAll("{", " ");  

I've tried escaping the curly brace using:

String n = s.replaceAll("/{", " "); 
String n = s.replaceAll("'{'", " "); 

None of this works, as it comes up with an error. Does anyone know a solution?

Upvotes: 8

Views: 38722

Answers (3)

Sanket Mehta
Sanket Mehta

Reputation: 622

Just adding to the answer above:

If somebody is trying like below, this won't work:

 if(values.contains("\\{")){
        values = values.replaceAll("\\{", "");
    }
    if(values.contains("\\}")){
        values = values.replaceAll("\\}", "");
    }

Use below code if you are using contains():

if(values.contains("{")){
        values = values.replaceAll("\\{", "");
    }
    if(values.contains("}")){
        values = values.replaceAll("\\}", "");
    }

Upvotes: 3

Gianmarco
Gianmarco

Reputation: 2552

you cannot define a String like this:

String s = "{aaa,":"bbb,ID":"ccc,}";

The error is here, you have to escape the double quotes inside the string, like this:

String s = "{aaa,\":\"bbb,ID\":\"ccc,}";

Now there will be no error if you call

s.replaceAll("\\{", " ");

If you have an IDE (that is a program like eclipse), you will notice that a string is colored different from the standard color black (for example the color of a method or a semicolon [;]). If the string is all of the same color (usually brown, sometimes blue) then you should be ok, if you notice some black color inside, you are doing something wrong. Usually the only thing that you would put after a double quote ["] is a plus [+] followed by something that has to be added to the string. For example:

String firstPiece = "This is a ";
// this is ok:
String all = s + "String";
//if you call:
System.out.println(all);
//the output will be: This is a String

// this is not ok:
String allWrong = s "String";
//Even if you are putting one after the other the two strings, this is forbidden and is a Syntax error.

Upvotes: 23

Alex
Alex

Reputation: 13941

String.replaceAll() takes a regex, and regex requires escaping of the '{' character. So, replace:

s.replaceAll("{", " ");

with:

s.replaceAll("\\{", " ");

Note the double-escapes - one for the Java string, and one for the regex.

However, you don't really need a regex here since you're just matching a single character. So you could use the replace method instead:

s.replace("{", " "); // Replace string occurrences
s.replace('{', ' '); // Replace character occurrences

Or, use the regex version to replace both braces in one fell swoop:

s.replaceAll("[{}]", " ");

No escaping is needed here since the braces are inside a character class ([]).

Upvotes: 19

Related Questions