I searched for SO (and Google) but did not find a complete answer to my question:
I want to replace all Swedish characters and spaces in String with another character. I would like it to work as follows:
- "å" and "ä" should be replaced by "a"
- "ö" should be replaced by "o"
- “Å” and “Ä” should be replaced by “A”
- "..." should be replaced by "O"
- "should be replaced by" - "
Is it possible to do this with a regular expression (or in any other way), and if so, how?
Of course, the method below 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 a regular expression to replace, for example, all "å", "ä" or "ö" with "". Question: how to replace a character using regular expression with another, depending on what character it is ? Of course, is it better to use regex than the above aproach?
source share