如果我正在使用Java流,并最终得到 IntStream 字符的code point数字的Unicode,那么如何呈现 CharSequence 这样的 String 呢?

String output = "input_goes_here".codePoints(). ??? ;

我已经在多个接口(interface)和类上找到了codePoints()方法,这些方法均会生成代码点的IntStream。但是我还没有找到任何接受相同方法的构造函数或工厂方法。
  • CharSequence::codePoints() → IntStream
  • String::codePoints() → IntStream
  • ojit_a

  • 我在寻找相反的东西:

    ➥如何从代码点的StringBuilder::codePoints() → IntStream实例化StringCharSequence等?

    最佳答案

    结合使用 IntStream::collect StringBuilder

    String output =
        "input_goes_here"
        .codePoints()                            // Generates an `IntStream` of Unicode code points, one `Integer` for each character in the string.
        .collect(                                // Collect the results of processing each code point.
            StringBuilder::new,                  // Supplier<R> supplier
            StringBuilder::appendCodePoint,      // ObjIntConsumer<R> accumulator
            StringBuilder::append                // BiConsumer<R,​R> combiner
        )
        .toString()
    ;
    

    如果您希望使用更通用的 CharSequence 接口(interface)而不是具体的 String 接口(interface),则只需在最后添加toString()即可。返回的 StringBuilder CharSequence
    IntStream codePointStream = "input_goes_here".codePoints ();
    CharSequence output = codePointStream.collect ( StringBuilder :: new , StringBuilder :: appendCodePoint , StringBuilder :: append );
    

    关于java - 从代码点编号的IntStream生成字符串?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57318362/

    10-09 01:13