Java 8 中的 StringJoiner length() 方法
length()Java8中StringJoiner类的方法用于获取StringJoiner当前值的长度。
要在Java8中使用StringJoiner,请导入以下包:
import java.util.StringJoiner;
语法如下:
public int length()
首先,创建一个StringJoiner:
StringJoiner strJoin = new StringJoiner(",");
strJoin.add("US");
strJoin.add("India");
strJoin.add("Netherlands");
strJoin.add("Australia");现在,使用以下length()方法找到StringJoiner的长度,即其中的元素数:
strJoin.length());
以下是length()在Java中实现StringJoiner方法的示例:
示例
import java.util.StringJoiner;
public class Demo {
public static void main(String[] args) {
//字符串连接器
StringJoiner strJoin = new StringJoiner(",");
strJoin.add("US");
strJoin.add("India");
strJoin.add("Netherlands");
strJoin.add("Australia");
System.out.println("Length of the StringJoiner = "+strJoin.length());
System.out.println("StringJoiner = "+strJoin.toString());
}
}输出结果Length of the StringJoiner = 30 StringJoiner = US,India,Netherlands,Australia