2

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.

Raptor
  • 51,208
  • 43
  • 217
  • 353
Tanu Garg
  • 2,767
  • 4
  • 19
  • 29

2 Answers2

10

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

Pshemo
  • 118,400
  • 24
  • 176
  • 257
4

Use this

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

here "\"" and "\\" are for " and \

Bohemian
  • 389,931
  • 88
  • 552
  • 692
Ruchira Gayan Ranaweera
  • 33,712
  • 16
  • 72
  • 110