简体   繁体   中英

how to remove special characters from string

I have a String called

String s = "Constitución Garantía";

I want to convert it to Constitución garantía .

This is a Spanish keyword. How can I convert it?

What you have described is an XY problem. It's the encoding issue and there might appear more of the characters that need to be replaced. Instead of replacing them one by one, you need to encode the whole String to UTF-8 .

String s = "Constitución Garantía"; 
byte[] ptext = s.getBytes(StandardCharsets.ISO_8859_1); 
String string = new String(ptext, StandardCharsets.UTF_8);      
System.out.println(string);                                 // Constitución Garantía

Consider fixing the encoding of a source where the string comes from before you actually start to work with it.

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