當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java StringJoiner merge()用法及代碼示例


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


相關用法


注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 StringJoiner merge() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。