原文: https://beginnersbook.com/2017/10/java-8-stringjoiner/

在 java 8 中,java.util包中引入了一个新类StringJoiner。使用这个类,我们可以使用指定的分隔符连接多个字符串,我们还可以在连接多个字符串时为最终字符串提供前缀和后缀。在本教程中,我们将看到几个StringJoiner类的示例,在本指南的最后,我们将看到StringJoiner类的方法。

Java StringJoiner示例 1:通过指定分隔符来连接字符串

在这个例子中,我们使用StringJoiner连接多个字符串。在创建StringJoiner的实例时,我们将分隔符指定为连字符(-)。

  1. import java.util.StringJoiner;
  2. public class Example {
  3. public static void main(String[] args) {
  4. // Passing Hyphen(-) as delimiter
  5. StringJoiner mystring = new StringJoiner("-");
  6. // Joining multiple strings by using add() method
  7. mystring.add("Logan");
  8. mystring.add("Magneto");
  9. mystring.add("Rogue");
  10. mystring.add("Storm");
  11. // Displaying the output String
  12. System.out.println(mystring);
  13. }
  14. }

输出:

  1. Logan-Magneto-Rogue-Storm

Java StringJoiner示例 2:为输出字符串添加前缀和后缀

  1. import java.util.StringJoiner;
  2. public class Example {
  3. public static void main(String[] args) {
  4. /* Passing comma(,) as delimiter and opening bracket
  5. * "(" as prefix and closing bracket ")" as suffix
  6. */
  7. StringJoiner mystring = new StringJoiner(",", "(", ")");
  8. // Joining multiple strings by using add() method
  9. mystring.add("Negan");
  10. mystring.add("Rick");
  11. mystring.add("Maggie");
  12. mystring.add("Daryl");
  13. // Displaying the output String
  14. System.out.println(mystring);
  15. }
  16. }

输出:

  1. (Negan,Rick,Maggie,Daryl)

StringJoiner示例 3:合并两个StringJoiner对象

  1. import java.util.StringJoiner;
  2. public class Example {
  3. public static void main(String[] args) {
  4. /* Passing comma(,) as delimiter and opening bracket
  5. * "(" as prefix and closing bracket ")" as suffix
  6. */
  7. StringJoiner mystring = new StringJoiner(",", "(", ")");
  8. mystring.add("Negan");
  9. mystring.add("Rick");
  10. mystring.add("Maggie");
  11. mystring.add("Daryl");
  12. System.out.println("First String: "+mystring);
  13. /* Passing hyphen(-) as delimiter and string "pre"
  14. * as prefix and string "suff" as suffix
  15. */
  16. StringJoiner myanotherstring = new StringJoiner("-", "pre", "suff");
  17. myanotherstring.add("Sansa");
  18. myanotherstring.add("Imp");
  19. myanotherstring.add("Jon");
  20. myanotherstring.add("Ned");
  21. System.out.println("Second String: "+myanotherstring);
  22. /* Merging both the strings
  23. * The important point to note here is that the output string will be
  24. * having the delimiter prefix and suffix of the first string (the string
  25. * which is calling the merge method of StringJoiner)
  26. */
  27. StringJoiner mergedString = mystring.merge(myanotherstring);
  28. System.out.println(mergedString);
  29. }
  30. }

输出:

  1. First String: (Negan,Rick,Maggie,Daryl)
  2. Second String: preSansa-Imp-Jon-Nedsuff
  3. (Negan,Rick,Maggie,Daryl,Sansa-Imp-Jon-Ned)

在上面的例子中,我们已经看到了StringJoiner类的add()merge()方法。让我们看看这个类的其他方法。

StringJoiner示例:setEmptyValue()length()toString()方法

  1. import java.util.StringJoiner;
  2. public class Example {
  3. public static void main(String[] args) {
  4. //Comma(,) as delimiter
  5. StringJoiner mystring = new StringJoiner(",");
  6. /* Using setEmptyValue() method, we can set the default value
  7. * of a StringJoiner instance, so if the StringJoiner is empty
  8. * and we print the value of it, this default value will be
  9. * displayed
  10. */
  11. mystring.setEmptyValue("This is a default String");
  12. /* We have not added any string to StringJoiner yet so
  13. * this should display the default value of StringJoiner
  14. */
  15. System.out.println("Default String: "+mystring);
  16. // Adding strings to StringJoiner
  17. mystring.add("Apple");
  18. mystring.add("Banana");
  19. mystring.add("Orange");
  20. mystring.add("Kiwi");
  21. mystring.add("Grapes");
  22. System.out.println(mystring);
  23. /* The length() method of StringJoiner class returns the
  24. * length of the string (the number of characters in the
  25. * StringJoiner instance)
  26. */
  27. int length = mystring.length();
  28. System.out.println("Length of the StringJoiner: "+length);
  29. /* The toString() method is used for converting a StringJoiner
  30. * instance to a String.
  31. */
  32. String s = mystring.toString();
  33. System.out.println(s);
  34. }
  35. }

输出:

  1. Default String: This is a default String
  2. Apple,Banana,Orange,Kiwi,Grapes
  3. Length of the StringJoiner: 31
  4. Apple,Banana,Orange,Kiwi,Grapes

参考文献:

Java 8 - StringJoiner JavaDoc