标签:should port cal inter rgs character 程序 list public
[StringJoiner是java.util包中的一个类,用于构造一个由分隔符分隔的字符序列(可选),并且可以从提供的前缀开始并以提供的后缀结尾。虽然这也可以在StringBuilder类的帮助下在每个字符串之后附加分隔符,但StringJoiner提供了简单的方法来实现,而无需编写大量代码。
Syntax : public StringJoiner(CharSequence delimiter) Parameters : delimiter - the sequence of characters to be used between each element added to the StringJoiner value Throws: NullPointerException - if delimiter is null
Syntax : public StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix) Parameters : delimiter - the sequence of characters to be used between each element added to the StringJoiner value prefix - the sequence of characters to be used at the beginning suffix - the sequence of characters to be used at the end Throws: NullPointerException - if prefix, delimiter, or suffix is null
Syntax : public String toString() Parameters : NA Returns : the string representation of this StringJoiner Overrides : toString in class Object
Syntax : public StringJoiner add(CharSequence newElement) Parameters : newElement - The element to add Returns : a reference to this StringJoiner
Syntax : public StringJoiner merge(StringJoiner other) Parameters : other - The StringJoiner whose contents should be merged into this one Returns : This StringJoiner Throws : NullPointerException - if the other StringJoiner is null
Syntax : public int length() Parameters : NA Returns : This StringJoiner
Syntax : public StringJoiner setEmptyValue(CharSequence emptyValue) Parameters : emptyValue - the characters to return as the value of an empty StringJoiner Returns : this StringJoiner itself so the calls may be chained Throws: NullPointerException - when the emptyValue parameter is null
下面是演示所有方法的java程序。
// Java program to demonstrate methods // of StringJoiner class ? import java.util.ArrayList; import java.util.StringJoiner; ????? public class Test2 { ????public static void main(String[] args) ????{ ???????ArrayList<String> al = new ArrayList<>(); ???????? ???????al.add("Ram"); ???????al.add("Shyam"); ???????al.add("Alice"); ???????al.add("Bob"); ???????? ???????StringJoiner sj1 = new StringJoiner(","); ???????? ???????// setEmptyValue() method ???????sj1.setEmptyValue("sj1 is empty"); ???????System.out.println(sj1); ???????? ???????// add() method ???????sj1.add(al.get(0)).add(al.get(1)); ???????System.out.println(sj1); ???????? ???????// length() method ???????System.out.println("Length of sj1 : " + sj1.length()); ???????? ???????StringJoiner sj2 = new StringJoiner(":"); ???????sj2.add(al.get(2)).add(al.get(3)); ???????? ???????//merge() method ???????sj1.merge(sj2); ???????? ???????// toString() method ???????System.out.println(sj1.toString()); ???????? ???????System.out.println("Length of new sj1 : " + sj1.length()); ????? ????} }
输出:
sj1 is empty Ram,Shyam Length of sj1 : 9 Ram,Shyam,Alice:Bob Length of new sj1 : 19??Java 字符串
标签:should port cal inter rgs character 程序 list public
原文地址:https://www.cnblogs.com/breakyizhan/p/13286155.html