英文:
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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论