Java流将字符转换为ASCII

huangapple go评论65阅读模式
英文:

Java Stream converting char to ascii

问题

所以我想要的只是打印字符而不是ASCII值...

     str.chars()
	 	.distinct()
	 	.forEach(System.out::println);

这是输出结果:

     97
     98
     99
     100

有人知道如何修复吗?

英文:

so I all I want for this to do is to print out the chars instead of the ascii values...

     str.chars()
	 	.distinct()
	 	.forEach(System.out::println);

this is the output:

     97
     98
     99
     100

does anyone know how to fix this?

答案1

得分: 1

你可以使用mapToObj方法相应地映射字符。

str.chars().mapToObj(c -> (char)c).forEach(System.out::println);
英文:

You can map the characters accordingly using the mapToObj method

str.chars().mapToObj(c -> (char)c).forEach(System.out::println);

答案2

得分: 0

你可以通过将 int 强制转换为 char 来实现如下:

str.chars()
    .distinct()
    .forEach(x -> System.out.println((char)x));
英文:

You can do this by casting the int to char as below

str.chars()
        .distinct()
        .forEach(x -> System.out.println((char)x));

huangapple
  • 本文由 发表于 2020年8月14日 14:08:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63407444.html
匿名

发表评论

匿名网友

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

确定