StringJoiner的merge(StringJoiner other)如果不为空,则将不带前缀和后缀的给定StringJoiner的内容添加为下一个元素。如果给定的StringJoiner为空,则该调用无效。
用法:
public StringJoiner merge(StringJoiner other)
参数:此方法接受一个强制性参数other,该参数是StringJoiner,其内容应合并到此参数中
返回值:此方法返回此StringJoiner
异常:如果另一个StringJoiner为null,则此方法引发NullPointerException
以下示例程序旨在说明merge()方法:
示例1:演示带有定界符“”的merge()
// Java program to demonstrate
// merge() method of StringJoiner
import java.util.StringJoiner;
public class GFG {
public static void main(String[] args)
{
// Creating StringJoiner with delimeter " "
StringJoiner str1 = new StringJoiner(" ");
// Adding elements in the StringJoiner
str1.add("Geeks");
str1.add("for");
str1.add("Geeks");
// Print the StringJoiner
System.out.println("StringJoiner 1: "
+ str1.toString());
// Creating the second StringJoiner
StringJoiner str2 = new StringJoiner(" ");
str2.add("A");
str2.add("Computer");
str2.add("Portal");
// Print the second StringJoiner
System.out.println("StringJoiner 2: "
+ str2.toString());
// Merging the StringJoiner using merge()
StringJoiner str = str1.merge(str2);
// Printing the merged StringJoiner
System.out.println("Merged StringJoiner : "
+ str);
}
}
输出:
StringJoiner 1: Geeks for Geeks StringJoiner 2: A Computer Portal Merged StringJoiner : Geeks for Geeks A Computer Portal
示例2:演示带有分隔符“,”的merge()
// Java program to demonstrate
// merge() method of StringJoiner
import java.util.StringJoiner;
public class GFG1 {
public static void main(String[] args)
{
// Creating StringJoiner with delimeter ", "
StringJoiner str1 = new StringJoiner(", ");
// Adding elements in the StringJoiner
str1.add("Geeks");
str1.add("for");
str1.add("Geeks");
// Print the StringJoiner
System.out.println("StringJoiner 1: "
+ str1.toString());
// Creating the second StringJoiner
StringJoiner str2 = new StringJoiner(", ");
str2.add("A");
str2.add("Computer");
str2.add("Portal");
// Print the StringJoiner
System.out.println("StringJoiner 2: "
+ str2.toString());
// Merging the StringJoiner using merge()
StringJoiner str = str1.merge(str2);
// Printing the merged StringJoiner
System.out.println("Merged StringJoiner : "
+ str);
}
}
输出:
StringJoiner 1: Geeks, for, Geeks StringJoiner 2: A, Computer, Portal Merged StringJoiner : Geeks, for, Geeks, A, Computer, Portal
示例3:演示NullPointerException
// Java program to demonstrate
// merge() method of StringJoiner
import java.util.StringJoiner;
public class GFG1 {
public static void main(String[] args)
{
// Creating StringJoiner with delimeter ", "
StringJoiner str1 = new StringJoiner(", ");
// Adding elements in the StringJoiner
str1.add("Geeks");
str1.add("for");
str1.add("Geeks");
// Print the StringJoiner
System.out.println("StringJoiner 1: "
+ str1.toString());
// Creating the second StringJoiner
// to be merged as null
StringJoiner str2 = null;
// Print the StringJoiner
System.out.println("StringJoiner 2: "
+ str2);
try {
// Merging the StringJoiner using merge()
StringJoiner str = str1.merge(str2);
}
catch (Exception e) {
System.out.println("Exception during merge: "
+ e);
}
}
}
输出:
StringJoiner 1: Geeks, for, Geeks StringJoiner 2: null Exception during merge: java.lang.NullPointerException
相关用法
- Java StringJoiner add()用法及代码示例
- Java StringJoiner setEmptyValue()用法及代码示例
- Java StringJoiner length()用法及代码示例
- Java StringJoiner toString()用法及代码示例
- Java HashMap merge(key, value, BiFunction)用法及代码示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代码示例
- Java Java lang.Long.byteValue()用法及代码示例
注:本文由纯净天空筛选整理自Code_r大神的英文原创作品 StringJoiner merge() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。