簡體   English   中英

如何刪除字符串中的特殊字符

[英]How to remove special characters in 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] ..

如何刪除"[""]"

只是用這個

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

您可以嘗試下一個:

// 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);

將它們放在轉義字符\\中的字符類[]

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

或者一個接一個地傳送它們(自己輸入其他字符:)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM