In java is there a way to replace specific special characters with another special characters within entire text without using if else .
Eg:
String s = abcd&c!&%^ .
Replace & with ~
Replace ! with ¬ etc on the above example string.
In java is there a way to replace specific special characters with another special characters within entire text without using if else .
Eg:
String s = abcd&c!&%^ .
Replace & with ~
Replace ! with ¬ etc on the above example string.
String has a replace function, so you can do s = s.replace('&','~');
public String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar.
s = s.replace('A','B').replace('C','D');s = s.replace('A','B'); if there is no 'A' in your String, nothing is going to get replaced. s will stay the same.String.replace(char oldChar, char newChar);
All things you can do with String: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html
I recommend reading these docs when you're working with anything Java you don't know. Like Arrays, or Lists, and so on.