Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a program which exchanges the letters "e" and "o" in one string using the `replace` method

For example, if I have Hello World it should become Holle Werld. How can I do this using String.replace? I've tried doing "Hello World".replace("e","o") but I only get Hollo World and if I use it again I will get Helle Werld.

like image 317
user10610048 Avatar asked Nov 05 '18 21:11

user10610048


1 Answers

You could also do:

String result = Arrays.stream(input.split(""))
                      .map(c -> c.equals("e") ? "o" : c.equals("o") ? "e" : c)
                      .collect(Collectors.joining());
like image 172
Ousmane D. Avatar answered Sep 28 '22 14:09

Ousmane D.