java Java用UTF-8字符编码字符串中的特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40814189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java encode special character in a String with UTF-8 character
提问by vyeluri5
String original = "This is my string valúe";
I'm trying to encode the above string to UTF-8 equivalent but to replace only special character (ú) with -- "ú ;" in this case.
我正在尝试将上述字符串编码为等效的 UTF-8,但仅将特殊字符 (ú) 替换为 -- "ú ;" 在这种情况下。
I've tried using the below but I get an error:
我已尝试使用以下内容,但出现错误:
Input is not proper UTF-8, indicate encoding !Bytes: 0xFA 0x20 0x63 0x61
输入的 UTF-8 不正确,表示编码 !Bytes: 0xFA 0x20 0x63 0x61
Code:
代码:
String original = new String("This is my string valúe");
byte ptext[] = original.getBytes("UTF-8");
String value = new String(ptext, "UTF-8");
System.out.println("Output : " + value);
This is my string valúe
回答by Elliott Frisch
You could use String.replace(CharSequence, CharSequence)
and formatted io like
您可以使用String.replace(CharSequence, CharSequence)
和格式化 io 之类的
String original = "This is my string valúe";
System.out.printf("Output : %s%n", original.replace("ú", "ú"));
Which outputs (as I think you wanted)
哪些输出(我认为你想要)
Output : This is my string valúe
回答by bmargulies
You seem to want to use XML character entities.
您似乎想使用 XML 字符实体。
Appache Commons Langhas a method for this (in StringEscapeUtils).
Appache Commons Lang有一个方法(在 StringEscapeUtils 中)。
回答by Walker Case
Im trying to encode the above string to UTF-8 equivalent but to replace only >special character ( ú ) with -- "ú ;" in this case.
我试图将上述字符串编码为等效的 UTF-8,但仅将 > 特殊字符 ( ú ) 替换为 -- "ú ;" 在这种情况下。
I'm not sure what encoding "ú ;" is but have you tried looking at the URLEncoder
class? It won't encode the string exactly the way you asked but it gets rid of the spooky character.
我不确定什么编码“ú ;” 但是你有没有试过看URLEncoder
课程?它不会完全按照您要求的方式对字符串进行编码,但它会去除令人毛骨悚然的字符。
回答by kishore enumula
Could you please try the below lines:
您能否尝试以下几行:
byte ptext[] = original.getBytes("UTF8");
String value = new String(ptext, "UTF8");