[英]Removing special character from Java String
I am trying to work around to remove symbols and special characters from a raw text in java and could not find way around. 我正在努力解决从java中的原始文本中删除符号和特殊字符并且无法找到方法。 The text is taken from a free-text field in a website which may contain literally anything.
该文本来自网站中的自由文本字段,该字段可能包含任何字面意思。 I am taking this text from an external source and have no control to change setting.
我从外部源获取此文本,无法控制更改设置。 So I have to work around at my end.
所以我必须在我的最后工作。 Some examples are
一些例子是
1) belem 🐺 should be--> belem 1)贝伦🐺应该是 - >贝伦
2) Ariana 👑 should be--> Ariana 2)阿丽亚娜👑应该是 - >阿丽亚娜
3) Harlem 🌊 should be--> Harlem 3)哈林🌊应该是 - >哈林
4) Yz 🏳️🌈 should be--> Yz 4)Yz🏳️🌈应该是 - > Yz
5) ここさけは7回は見に行くぞ👍💟 should be--> ここさけは7回は見に行くぞ 5)ここさけは7回は见に行くぞ👍💟应该是 - >ここさけは7回は见に行くぞ
6) دمي ازرق وطني ازرق 💙🔵🔵🔵🔵 should be--> دمي ازرق وطني ازرق 6)应该是 - >دميازرقوطنيازرق
Any help please? 有什么帮助吗?
You can try this regex that find all emojis in a string : 你可以尝试这个在字符串中找到所有表情符号的正则表达式:
regex = "[\\ud83c\\udc00-\\ud83c\\udfff]|[\\ud83d\\udc00-\\ud83d\\udfff]|[\\u2600-\\u27ff]"
then remove all the emojis in it using replaceAll()
method: 然后使用
replaceAll()
方法删除其中的所有表情符号:
String text = "ここさけは7回は見に行くぞ👍💟 ";
String regex = "[\\ud83c\\udc00-\\ud83c\\udfff]|[\\ud83d\\udc00-\\ud83d\\udfff]|[\\u2600-\\u27ff]";
System.out.println(text.replaceAll(regex, ""));
Output : 输出 :
ここさけは7回は見に行くぞ
If you mean "special characters" are surrogate pairs, try this. 如果你的意思是“特殊字符”是代理对,试试这个。
static String removeSpecial(String s) {
int[] r = s.codePoints()
.filter(c -> c < Character.MIN_SURROGATE)
.toArray();
return new String(r, 0, r.length);
}
and 和
String[] testStrs = {
"belem 🐺",
"Ariana 👑",
"Harlem 🌊",
"Yz 🏳️🌈",
"ここさけは7回は見に行くぞ👍💟",
"دمي ازرق وطني ازرق 💙🔵🔵🔵🔵"
};
for (String s : testStrs)
System.out.println(removeSpecial(s));
results 结果
belem
Ariana
Harlem
Yz
ここさけは7回は見に行くぞ
دمي ازرق وطني ازرق
对于空格使用字符类,对“任何语言的任何字母或数字”使用POSIX字符类:
str = str.replaceAll("[^\\s\\p{Alnum}]", "");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.