如何定义字符串数组java的特殊字符 [英] How to define special characters in string array java

查看:101
本文介绍了如何定义字符串数组java的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要定义包含以下所有特殊字符数组。

I need to define an array containing below all special characters..

+ - && || ! ( ) { } [ ] ^ " ~ * ? : \

我用这

List<String> specialCharactersInSolr = Arrays.asList(new String[] {
                "+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^",
                "~", "*", "?", ":", });

据接受所有的字符除了和\\

It is accepting all the character except " and \

请帮忙如何界定这两个为好。

Please help how to define these two as well.

推荐答案

\\ 是特殊字符String类

\ and " are special characters in String class


  • 是开始或结束字符串

  • \\ 用于创建如新行的某些字符 \\ n \\ r 设置页 \\ 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 "\""

其他的想法是使用字符[] 而不是的String [] 所以你不会有逃避和你的角色可以写成''\\\\'(因为 需要逃避 - 它应该写成'\\'' - \\ 还特别在这里,也将需要转义生产它的字面)。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆