Java String join方法示例
时间:2020-02-23 14:35:13 来源:igfitidea点击:
String的 join方法可用于通过分隔符连接各种字符串。
它是在Java 8中引入的。
例如:我们需要加入"各种字符串","。
我们可以使用Join方法
String.join(",","java","world","tutorials"); //Output "java,world,tutorial"
String的join方法有两个重载变体:
public static String join(CharSequence delimiter, CharSequence... elements) and public static String join(CharSequence delimiter, Iterable elements)
字符串连接示例:
package org.igi.theitroad; public class StringJoinExample { public static void main(String[] args) { System.out.println(String.join(",","java","world","tutorials")); System.out.println(String.join("-","java","world","tutorials")); } }
运行上面的程序时,我们将得到以下输出:
java,world,tutorials java-world-tutorials