I want to know how to replace the string in Java.
我想知道如何在Java中替换字符串。
E.g.
例如。
String a = "adf�sdf";
How can I replace and avoid special characters?
如何更换和避免特殊字符?
4 个解决方案
#1
14
You can get rid of all characters outside the printable ASCII range using String#replaceAll()
by replacing the pattern [^\\x20-\\x7e]
with an empty string:
您可以使用String#replaceAll()删除可打印ASCII范围之外的所有字符,方法是将模式[^ \\ x20 - \\ x7e]替换为空字符串:
a = a.replaceAll("[^\\x20-\\x7e]", "");
But this actually doesn't solve your actual problem. It's more a workaround. With the given information it's hard to nail down the root cause of this problem, but reading either of those articles must help a lot:
但这实际上并不能解决您的实际问题。这是一种解决方法。根据给定的信息,很难确定这个问题的根本原因,但阅读其中任何一篇文章都必须提供很多帮助:
- The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
- 绝对最低每个软件开发人员绝对必须知道Unicode和字符集(没有借口!)
- Unicode - How to get the characters right?
- Unicode - 如何使角色正确?
#2
2
It is hard to answer the question without knowing more of the context.
在不了解更多背景的情况下很难回答这个问题。
In general you might have an encoding problem. See The Absolute Minimum Every Software Developer (...) Must Know About Unicode and Character Sets for an overview about character encodings.
通常,您可能遇到编码问题。有关字符编码的概述,请参阅绝对最低每个软件开发人员(...)必须了解Unicode和字符集。
#3
2
Assuming, that you want to remove all special characters, you can use the character class \p{Cntrl}
Then you only need to use the following code:
假设您要删除所有特殊字符,可以使用字符类\ p {Cntrl}然后您只需要使用以下代码:
stringWithSpecialCharcters.replaceAll("\\p{Cntrl}", replacement);
#4
0
You can use unicode escape sequences (such as \u201c
[an opening curly quote]) to "avoid" characters that can't be directly used in your source file encoding (which defaults to the default encoding for your your platform, but you can change it with the -encoding
parameter to javac
).
您可以使用unicode转义序列(例如\ u201c [开头的引号]来“避免”无法直接在源文件编码中使用的字符(默认为您的平台的默认编码,但您可以使用-encoding参数将其更改为javac)。
#1
14
You can get rid of all characters outside the printable ASCII range using String#replaceAll()
by replacing the pattern [^\\x20-\\x7e]
with an empty string:
您可以使用String#replaceAll()删除可打印ASCII范围之外的所有字符,方法是将模式[^ \\ x20 - \\ x7e]替换为空字符串:
a = a.replaceAll("[^\\x20-\\x7e]", "");
But this actually doesn't solve your actual problem. It's more a workaround. With the given information it's hard to nail down the root cause of this problem, but reading either of those articles must help a lot:
但这实际上并不能解决您的实际问题。这是一种解决方法。根据给定的信息,很难确定这个问题的根本原因,但阅读其中任何一篇文章都必须提供很多帮助:
- The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
- 绝对最低每个软件开发人员绝对必须知道Unicode和字符集(没有借口!)
- Unicode - How to get the characters right?
- Unicode - 如何使角色正确?
#2
2
It is hard to answer the question without knowing more of the context.
在不了解更多背景的情况下很难回答这个问题。
In general you might have an encoding problem. See The Absolute Minimum Every Software Developer (...) Must Know About Unicode and Character Sets for an overview about character encodings.
通常,您可能遇到编码问题。有关字符编码的概述,请参阅绝对最低每个软件开发人员(...)必须了解Unicode和字符集。
#3
2
Assuming, that you want to remove all special characters, you can use the character class \p{Cntrl}
Then you only need to use the following code:
假设您要删除所有特殊字符,可以使用字符类\ p {Cntrl}然后您只需要使用以下代码:
stringWithSpecialCharcters.replaceAll("\\p{Cntrl}", replacement);
#4
0
You can use unicode escape sequences (such as \u201c
[an opening curly quote]) to "avoid" characters that can't be directly used in your source file encoding (which defaults to the default encoding for your your platform, but you can change it with the -encoding
parameter to javac
).
您可以使用unicode转义序列(例如\ u201c [开头的引号]来“避免”无法直接在源文件编码中使用的字符(默认为您的平台的默认编码,但您可以使用-encoding参数将其更改为javac)。