Replace a character with different characters depending on which character it is

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?

+4
source share
4 answers

For Latin characters with diacritics, Unicode normalization (java text) may be required to extract the base letter code + diacritic code. Sort of:

 import java.text.Normalizer; newString = Normalizer.normalize(string, Normalizer.Form.NFKD).replaceAll("\\p{M}", ""); 
+4
source

You can use StringUtils.replaceEach , for example:

 private String changeSwedishCharactersAndWhitespace(String string) { String newString = StringUtils.replaceEach (string, new String[] {"å", "ä", "ö", "Å", "Ä", "Ö", " "}, new String[] {"a", "a", "o", "A", "A", "O", "-"}); return newString; } 
+3
source

I think that there is no general regular expression to replace these characters immediately. In addition, you can make replacement work easier with the HashMap .

 HashMap<String, String> map = new HashMap<String, String>() {{put("ä", "a"); /*put others*/}}; for (Map.Entry<String, String> entry : map.entrySet()) newString = string.replaceAll(entry.getKey(), entry.getValue()); 
+3
source

You can write your own matching method using the matcher.find method:

 public static void main(String[] args) { String from = "äöÂ"; String to = "aoA"; String testString = "Hellö Wärld"; Pattern p = Pattern.compile(String.format("[%s]", from)); Matcher m = p.matcher(testString); String result = testString; while (m.find()){ char charFound = m.group(0).charAt(0); result = result.replace(charFound, to.charAt(from.indexOf(charFound))); } System.out.println(result); } 

it will replace

 Hellö Wärld 

with

 Hello Warld 
0
source

Source: https://habr.com/ru/post/1446201/


All Articles