📜  Java中的StringJoiner merge()方法

📅  最后修改于: 2022-05-13 01:55:25.541000             🧑  作者: Mango

Java中的StringJoiner merge()方法

StringJoiner 的merge(StringJoiner other)如果不为空,则将不带前缀和后缀的给定 StringJoiner 的内容添加为下一个元素。如果给定的 StringJoiner 为空,则调用无效。
句法:

public StringJoiner merge(StringJoiner other)

参数:此方法接受一个强制参数other是 StringJoiner,其内容应合并到此
返回:此方法返回此 StringJoiner
异常:如果另一个 StringJoiner 为 null,则此方法抛出NullPointerException
下面的程序说明了 merge() 方法:
示例 1:演示带有分隔符“”的 merge()

Java
// Java program to demonstrate
// merge() method of StringJoiner
 
import java.util.StringJoiner;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Creating StringJoiner with delimiter " "
        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);
    }
}


Java
// Java program to demonstrate
// merge() method of StringJoiner
 
import java.util.StringJoiner;
 
public class GFG1 {
    public static void main(String[] args)
    {
 
        // Creating StringJoiner with delimiter ", "
        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);
    }
}


Java
// Java program to demonstrate
// merge() method of StringJoiner
 
import java.util.StringJoiner;
 
public class GFG1 {
    public static void main(String[] args)
    {
 
        // Creating StringJoiner with delimiter ", "
        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: A Computer Portal
Merged StringJoiner : Geeks for Geeks A Computer Portal

示例 2:演示带有分隔符“、”的 merge()

Java

// Java program to demonstrate
// merge() method of StringJoiner
 
import java.util.StringJoiner;
 
public class GFG1 {
    public static void main(String[] args)
    {
 
        // Creating StringJoiner with delimiter ", "
        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

// Java program to demonstrate
// merge() method of StringJoiner
 
import java.util.StringJoiner;
 
public class GFG1 {
    public static void main(String[] args)
    {
 
        // Creating StringJoiner with delimiter ", "
        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