In my java code, if any of the special characters were added to the string input, which should be preceded by \\
Special character set {+, -, &&, ||, !, (, ), {, },[, ], ^, "", ~, *, ?, :, \} . I tried using String.replaceAll(old,new) , but, to my surprise, it does not work, although I give the correct values ββfor the "old" and "new".
if old=":",new="\:"
I put special characters in the String array, repeated it in a for loop, checked if it was present in the string, if so, input.replaceAll(":","\\:") . But this does not give me the intended exit. Please, help
String[] arr = { "+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "\"", "~", "*", "?", ":", "\\", "AND", "OR" }; for (int i = 0; i < arr.length; i++) { //'search' is my input string if (search.contains((String) arr[i])) { String oldString = (String) arr[i]; String newString = new String("\\" + arr[i]); search = search.replaceAll(oldString, newString); String newSearch = new String(search.replaceAll(arr[i], newString)); } }