个性化阅读
专注于IT技术分析

Java StringJoiner

Java在java.util包中添加了一个新的最终类StringJoiner。它用于构造由定界符分隔的字符序列。现在, 你可以通过传递诸如comma(, ), hyphen(-)等分隔符来创建字符串。还可以将前缀和后缀传递给char序列。

StringJoiner构造函数

建设者 描述
Public StringJoiner(CharSequence delimiter) 它构造一个不带任何字符, 没有前缀或后缀的StringJoiner, 以及提供的定界符的副本。如果分隔符为null, 则抛出NullPointerException。
Public StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix) 它使用提供的前缀, 定界符和后缀的副本构造一个不带任何字符的StringJoiner。如果前缀, 定界符或后缀为null, 则抛出NullPointerException。

StringJoiner方法

方法 描述
Public StringJoiner add(CharSequence newElement) 它将给定CharSequence值的副本添加为StringJoiner值的下一个元素。如果newElement为null, 则添加“ null”。
Public StringJoiner merge(StringJoiner other) 如果它不是空的, 它将添加不带前缀和后缀的给定StringJoiner的内容作为下一个元素。如果给定的StringJoiner为空, 则该调用无效。
Public int length() 它返回此StringJoiner的String表示形式的长度。
Public StringJoiner setEmptyValue(CharSequence emptyValue) 它设置确定此StringJoiner的字符串表示形式时要使用的字符序列, 并且尚未添加任何元素(即, 当它为空时)。

Java StringJoiner示例

// importing StringJoiner class
import java.util.StringJoiner;
public class StringJoinerExample {
	public static void main(String[] args) {
		StringJoiner joinNames = new StringJoiner(", ");	// passing comma(, ) as delimiter 
		
		// Adding values to StringJoiner
		joinNames.add("Rahul");
		joinNames.add("Raju");
		joinNames.add("Peter");
		joinNames.add("Raheem");
				
		System.out.println(joinNames);
	}
}

输出:

Rahul, Raju, Peter, Raheem

Java StringJoiner示例:添加前缀和后缀

// importing StringJoiner class
import java.util.StringJoiner;
public class StringJoinerExample {
	public static void main(String[] args) {
		StringJoiner joinNames = new StringJoiner(", ", "[", "]");	// passing comma(, ) and square-brackets as delimiter 
		
		// Adding values to StringJoiner
		joinNames.add("Rahul");
		joinNames.add("Raju");
		joinNames.add("Peter");
		joinNames.add("Raheem");
				
		System.out.println(joinNames);
	}
}

输出:

[Rahul, Raju, Peter, Raheem]

StringJoiner示例:合并两个StringJoiner

merge()方法合并两个StringJoiner对象, 但不包括第二个StringJoiner对象的前缀和后缀。

// importing StringJoiner class
import java.util.StringJoiner;
public class StringJoinerExample {
	public static void main(String[] args) {

		StringJoiner joinNames = new StringJoiner(", ", "[", "]");	// passing comma(, ) and square-brackets as delimiter 
		
		// Adding values to StringJoiner
		joinNames.add("Rahul");
		joinNames.add("Raju");

		// Creating StringJoiner with :(colon) delimiter
		StringJoiner joinNames2 = new StringJoiner(":", "[", "]");	// passing colon(:) and square-brackets as delimiter 
		
		// Adding values to StringJoiner
		joinNames2.add("Peter");
		joinNames2.add("Raheem");

		// Merging two StringJoiner
		StringJoiner merge = joinNames.merge(joinNames2); 
		System.out.println(merge);
	}
}

输出:

[Rahul, Raju, Peter:Raheem]

StringJoiner示例:StringJoiner方法

// importing StringJoiner class
import java.util.StringJoiner;
public class StringJoinerExample {
	public static void main(String[] args) {
		StringJoiner joinNames = new StringJoiner(", ");	// passing comma(, ) as delimiter 
		
		// Prints nothing because it is empty
		System.out.println(joinNames);
		
		// We can set default empty value.
		joinNames.setEmptyValue("It is empty");
		System.out.println(joinNames);
		
		
		// Adding values to StringJoiner
		joinNames.add("Rahul");
		joinNames.add("Raju");
		System.out.println(joinNames);
		
		// Returns length of StringJoiner
		int length = joinNames.length();
		System.out.println("Length: "+length);
		
		// Returns StringJoiner as String type 
		String str = joinNames.toString();
		System.out.println(str);
		
		// Now, we can apply String methods on it
		char ch = str.charAt(3);
		System.out.println("Character at index 3: "+ch);
		
		// Adding one more element 
		joinNames.add("Sorabh");
		System.out.println(joinNames);
		
		// Returns length
		int newLength = joinNames.length();
		System.out.println("New Length: "+newLength);
	}
}

输出:

It is empty
Rahul, Raju
Length: 10
Rahul, Raju
Character at index 3: u
Rahul, Raju, Sorabh
New Length: 17
赞(0)
未经允许不得转载:srcmini » Java StringJoiner

评论 抢沙发

评论前必须登录!