I have searched SO (and Google) but not found any fully matching answer to my question:
I want to replace all swedish characters and whitespace in a String
with another character. I would like it to work as follows:
- "å" and "ä" should be replaced with "a"
- "ö" should be replaced with "o"
- "Å" and "Ä" should be replace with "A"
- "Ö" should be replaced with "O"
- " " should be replaced with "-"
Can this be achieved with regex (or any other way), and if so, how?
Of course, the below method does the job (and can be improved, I know, by replacing for example "å" and "ä" on the same line):
private String changeSwedishCharactersAndWhitespace(String string) {
String newString = string.replaceAll("å", "a");
newString = string.replaceAll("ä", "a");
newString = string.replaceAll("ö", "o");
newString = string.replaceAll("Å", "A");
newString = string.replaceAll("Ä", "A");
newString = string.replaceAll("Ö", "O");
newString = string.replaceAll(" ", "-");
return newString;
}
I know how to use regex to replace, for example, all "å", "ä", or "ö" with "". The question is how do I replace a character using regex with another depending on which character it is? There must surely be a better way using regex than the above aproach?