[英]How to make a String replacement program without any for loop in java?
For Example :- 例如 :-
String str = "Hello How are u!"
Replacement character with each other. 彼此替换字符。
a one
e two
i three
o four
u five
OutPut 产量
Htwollfour Hfourw onertwo five!
How do I do that without any looping ? 我该怎么做而没有任何循环?
One way is to use a stream. 一种方法是使用流。 And a map:
和地图:
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);
And that prints Htwollfour Hfourw onertwo five!
然后打印
Htwollfour Hfourw onertwo five!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.