简体   繁体   中英

How to remove special characters in String

I want to remove "[", "]" , "," from my string for example,

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

to

569.24 569.24 568.10 566.00 566.01 566.00 567.98 565.14

however, I can remove "," but "[" and "]"

my codes are as follows.

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

the above result in [569.24, 569.24, 568.10, 566.00, 566.01, 566.00, 567.98, 565.14] ..

How can I remove "[" and "]" ?

just use this

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

You can try the next:

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

Put them in character class [] with escape character \\

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

Or pipe them one by one(put other characters yourself :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM