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
  • 53,206
  • 45
  • 230
  • 366
Tanu Garg
  • 3,007
  • 4
  • 21
  • 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
  • 122,468
  • 25
  • 185
  • 269
4

Use this

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

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

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115