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

  • A+

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.


You could also do:

String result = Arrays.stream(input.split(""))                       .map(c -> c.equals("e") ? "o" : c.equals("o") ? "e" : c)                       .collect(Collectors.joining()); 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: