i use lucene search but lucene has a bunch of special characters to escape like:
- && || ! ( ) { } [ ] ^ \" ~ * ? : \\
i am havin
Use regular expression. String.replaceAll()
supports regular expression, so you can solve this problem using one single call. Just be careful: some of these characters are special for regular expressions too, so they mus be escaped "twice":
str.replaceAll("([-\\&\\|!\\(\\)\\{\\}\\[\\]\\^\\"~\\*\\?:\\])", "\\$1");
(I have not tried this, probably this line needs some fixes, but this is the idea)