String转为string java8
在Java 8中,可以使用Stream API将String[]转换为String。可以使用Stream的collect()方法,并使用Collectors.joining()方法将数组中的元素连接起来。
以下是将String[]转换为String的示例代码:
import java.util.Arrays;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String[] array = {"Hello", "World"};
String result = Arrays.stream(array)
.collect(Collectors.joining(" "));
System.out.println(result);
}
}
输出结果为:
Hello World
在这个示例中,我们使用Arrays.stream()方法将String[]数组转换为Stream。然后,我们使用Collectors.joining()方法将数组中的元素连接起来,并指定使用空格作为分隔符。最后,我们将结果打印出来
原文地址: https://www.cveoy.top/t/topic/isCp 著作权归作者所有。请勿转载和采集!