Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define special characters in string array java

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.

like image 497
Tanu Garg Avatar asked Jan 29 '26 12:01

Tanu Garg


2 Answers

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

like image 185
Pshemo Avatar answered Jan 31 '26 01:01

Pshemo


Use this

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

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

like image 34
Ruchira Gayan Ranaweera Avatar answered Jan 31 '26 01:01

Ruchira Gayan Ranaweera



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!