gpt4 book ai didi

java - 从代码点整数列表生成 String 对象?

转载 作者:行者123 更新时间:2023-12-02 02:04:37 25 4
gpt4 key购买 nike

如果我有一个 List< Integer >其整数值为 Unicode code point数字。如何构建 String由这些代码点确定的字符对象?

例如:

List < Integer > codePoints = List.of( 100, 111, 103, 128054 ) ;

...或:

List < Integer > codePoints = "cat".codePoints().boxed().toList();

如何获得另一个String具有值 cat 的对象来自codePoints

最佳答案

字符串 has a constructor它采用一个 int 代码点数字数组。

int[] ints = codePoints.stream().mapToInt(i -> i).toArray();
String string = new String(ints, 0, ints.length);

转换List -> Stream -> IntStream -> int[] -> String

关于java - 从代码点整数列表生成 String 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68606229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com