How to define special characters in string array java

arraylist, arrays, java

Solution

`\` and `"` are special characters in String class

- `"` 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 `"\""`

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).

Problem

I need to define an array containing below all special characters.. ``` + - && || ! ( ) { } [ ] ^ " ~ * ? : \ ``` I am using this ``` List<String> specialCharactersInSolr = Arrays.asList(new String[] { "+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "~", "*", "?", ":", }); ``` It is accepting all the character except " and \ Please help how to define these two as well.

Original source