简体   繁体   English

如何从字符串中删除特殊字符

[英]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 . 我想将它转换为Constitución garantía

This is a Spanish keyword. 这是一个西班牙语关键字。 How can I convert it? 我怎么转换它?

What you have described is an XY problem. 你所描述的是一个XY问题。 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编码为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. 在实际开始使用字符串之前,请考虑修改字符串来源的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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