例如 :-
String str = "Hello How are u!"
彼此替换字符。
a one e two i three o four u five
输出
Htwollfour Hfourw onertwo five!
我该怎么做而没有任何循环?
一种方法是使用流。和地图:
Map<Character, String> mapping = new HashMap<>(); mapping.put('a', "one"); mapping.put('e', "two"); mapping.put('i', "three"); mapping.put('o', "four"); mapping.put('u', "five"); String res = "Hello How are u!".chars() .mapToObj(ch -> mapping.getOrDefault((char) ch, Character.toString((char) ch))) .collect(Collectors.joining()); System.out.println(res);
那打印 Htwollfour Hfourw onertwo five!