如何删除String中的特殊字符

时间:2014-03-23 20:55:12

标签: java regex string

我想从字符串中删除"[", "]""," 例如,

[569.24, 569.24, 568.10, 566.00, 566.01, 566.00, 567.98, 565.14]

569.24 569.24 568.10 566.00 566.01 566.00 567.98 565.14

但是,我可以移除",""[""]"

我的代码如下。

String content = price_result.toString();           
//remove special characters
String content_modified = content.replaceAll("[ \t\"',;]+", " ");
System.out.println(content_modified);

上述结果[569.24, 569.24, 568.10, 566.00, 566.01, 566.00, 567.98, 565.14] ..

如何删除"[""]"

3 个答案:

答案 0 :(得分:1)

只需使用此

String content = price_result.toString();           
//remove special characters
String content_modified = content.replace("[","").replace("]","").replace(",","");
System.out.println(content_modified);

答案 1 :(得分:1)

您可以尝试下一个:

// Characters you want to remove
String unwanted = "[],";

// It will be used frequently? Use a constant.
Pattern pattern = Pattern.compile("[" + Pattern.quote(unwanted) + "]");

String content = price_result.toString();
String content_modified = pattern.matcher(content).replaceAll("");
System.out.println(content_modified);

答案 2 :(得分:0)

将它们放入带有转义字符[]

的字符类\
String content_modified = content.replaceAll("[\\[\t\"',;\\]]+", " ");

或逐个管道(自己放置其他字符:)

String content_modified = content.replaceAll("\\[|\\]|,|;", " ");