java如何在字符串数组中定义特殊字符 [英] How to define special characters in string array java
问题描述
我需要定义一个包含以下所有特殊字符的数组..
I need to define an array containing below all special characters..
+ - && || ! ( ) { } [ ] ^ " ~ * ? : \
我正在使用这个
List<String> specialCharactersInSolr = Arrays.asList(new String[] {
"+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^",
"~", "*", "?", ":", });
它接受除 " 和 \
请帮助如何定义这两个.
Please help how to define these two as well.
推荐答案
\
和 "
是 String 类中的特殊字符
\
and "
are special characters in String class
"
是字符串的开始或结束\
用于创建一些字符,如新行\n
\r
tab\t
或转义特殊字符,例如您的情况\
和"
"
is start or end of String\
is used to create some characters like new lines\n
\r
tab\t
or to escape special characters like in your case\
and"
所以要使它们成为文字,您必须使用 "\\"
和 "\""
So to make them literals you will have to escape them with "\\"
and "\""
其他想法是使用 Character[]
而不是 String[]
这样你就不必转义 "
并且你的字符可以被写入作为 '"'
或 '\\'
(因为 '
需要转义 - 它应该写成 '\''
code> - \
在这里也很特殊,也需要转义以生成其文字).
Other idea is to use Character[]
instead of String[]
so you wont have to escape "
and yours characters can be written as '"'
or '\\'
(because '
require escaping - it should be written as '\''
- \
is also special here and will also require escaping to produce its literal).
这篇关于java如何在字符串数组中定义特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!