在 Java 中删除大括号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18796166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove curly brace in Java
提问by user2778018
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?
这一切都不起作用,因为它出现了错误。有谁知道解决方案?
回答by Gianmarco
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:
如果您有 IDE(即 eclipse 之类的程序),您会注意到字符串的颜色与标准颜色黑色不同(例如方法的颜色或分号 [;])。如果字符串都是相同的颜色(通常是棕色,有时是蓝色)那么你应该没问题,如果你注意到里面有一些黑色,你做错了。通常,你会在双引号 ["] 后面加上一个加号 [+] 后跟一些必须添加到字符串中的内容。例如:
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.
回答by Alex
String.replaceAll() takes a regex, and regex requires escaping of the '{'
character. So, replace:
String.replaceAll() 采用正则表达式,正则表达式需要对'{'
字符进行转义。所以,替换:
s.replaceAll("{", " ");
with:
和:
s.replaceAll("\{", " ");
Note the double-escapes - one for the Java string, and one for the regex.
请注意双重转义 - 一个用于 Java 字符串,另一个用于正则表达式。
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:
但是,您在这里并不真正需要正则表达式,因为您只是匹配单个字符。所以你可以改用这个replace
方法:
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 ([]
).
这里不需要转义,因为大括号在字符类 ( []
) 内。
回答by Sanket Mehta
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():
如果您使用 contains(),请使用以下代码:
if(values.contains("{")){
values = values.replaceAll("\{", "");
}
if(values.contains("}")){
values = values.replaceAll("\}", "");
}