[英]Remove Square Brackets and replace with Curly Brackets
I am trying to replace all the square brackets with the curly brackets. 我正在尝试将所有方括号替换为大括号。
Example: 例:
String string = "{\"test\":{\"id\":["4,5,6"}]},{\"Tech\":["Java,C++"]}}";
I want to remove square brackets([,]) and replace them with curly brackets({,}). 我想删除方括号([,])并将其替换为大括号({,})。
Like: 喜欢:
"{\"test\":{\"id\":{"4,5,6"}}},{\"Tech\":{"Java,C++"}}}";
I have tried: 我努力了:
string = string.replace("\\[", "\\{").replace("\\]", "\\}");
But didnt worked.Need some suggestions here. 但是没有用,这里需要一些建议。
You don't need \\\\[
and \\\\{
for that you don't get the correct result, instead you can use : 您不需要
\\\\[
和\\\\{
因为您没有得到正确的结果,而是可以使用:
System.out.println(string.replace("[", "{").replace("]", "}"));
Note 注意
Your are using an invalid String you have to use \\
before "\\""
like this : 您使用的字符串无效,您必须在
"\\""
之前使用\\
,如下所示:
String string = "{\"test\":{\"id\":[\"4,5,6\"}]},{\"Tech\":[\"Java,C++\"]}}";
you can either do this: 您可以执行以下操作:
str.replace('[', '{').replace(']', '}');
or you can do this: 或者您可以这样做:
str.replaceAll("\\[", "{").replaceAll("\\]", "}");
Replace uses Characters, thus the ' ' whereas replaceAll uses Regex. Replace使用字符,因此使用',而replaceAll使用正则表达式。 This is why you should escape your bracket there.
这就是为什么您应该将支架放在那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.