java.lang.string.join()方法使用定界符將給定元素連接起來並返回連接的字符串。請注意,如果元素為null,則將添加null。自JDK 1.8開始,java字符串中包含join()方法。 java字符串中有兩種類型的join()方法。句法:
public static String join(CharSequence deli, CharSequence... ele) and public static String join (CharSequence deli, Iterable<? extends CharSequence> ele) 參數: deli- delimiter to be attached with each element ele- string or char to be attached with delimiter 返回: string joined with delimiter.
// Java program to demonstrate
// working of join() method
class Gfg1 {
public static void main(String args[])
{
// delimiter is "<" and elements are "Four", "Five", "Six", "Seven"
String gfg1 = String.join(" < ", "Four", "Five", "Six", "Seven");
System.out.println(gfg1);
}
}
輸出:
Four < Five < Six < Seven
// Java program to demonstrate
// working of join() method
class Gfg2 {
public static void main(String args[])
{
// delimiter is " " and elements are "My",
// "name", "is", "Niraj", "Pandey"
String gfg2 = String.join(" ", "My", "name", "is", "Niraj", "Pandey");
System.out.println(gfg2);
}
}
輸出:
My name is Niraj Pandey
// Java program to demonstrate
// working of join() method
class Gfg3 {
public static void main(String args[])
{
// delimiter is "->" and elements are "Wake up",
// "Eat", "Play", "Sleep", "Wake up"
String gfg3 = String.join("-> ", "Wake up", "Eat",
"Play", "Sleep", "Wake up");
System.out.println(gfg3);
}
}
輸出:
Wake up-> Eat-> Play-> Sleep-> Wake up
相關用法
- Java Guava Chars.join()用法及代碼示例
- Java Guava Floats.join()用法及代碼示例
- Java Guava Doubles.join()用法及代碼示例
- Java Guava Shorts.join()用法及代碼示例
- Java Guava Booleans.join()用法及代碼示例
- Java Guava Longs.join()用法及代碼示例
- Java Guava Ints join()用法及代碼示例
- Java String toLowerCase()用法及代碼示例
- Java String getChars()用法及代碼示例
- Java String format()用法及代碼示例
注:本文由純淨天空篩選整理自Niraj_Pandey大神的英文原創作品 Java String join() with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。