码迷,mamicode.com
首页 > 编程语言 > 详细

Java8中的Java.util.StringJoiner

时间:2020-07-12 00:44:15      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:should   port   cal   inter   rgs   character   程序   list   public   

[
  • ??Java 字符串

    Java8中的Java.util.StringJoiner

    StringJoiner是java.util包中的一个类,用于构造一个由分隔符分隔的字符序列(可选),并且可以从提供的前缀开始并以提供的后缀结尾。虽然这也可以在StringBuilder类的帮助下在每个字符串之后附加分隔符,但StringJoiner提供了简单的方法来实现,而无需编写大量代码。

    StringJoiner构造函数:

    1. StringJoiner(CharSequence delimiter):构造一个StringJoiner,其中没有字符,没有前缀或后缀,并提供一个提供的分隔符的副本。
      Syntax : 
      public StringJoiner(CharSequence delimiter)
      Parameters : 
      delimiter - the sequence of characters to be used between 
      each element added to the StringJoiner value
      Throws:
      NullPointerException - if delimiter is null
    2. StringJoiner(CharSequence分隔符,CharSequence前缀,CharSequence后缀):构造一个StringJoiner,其中没有字符,没有前缀或后缀,以及提供的分隔符的副本。
      Syntax : 
      public StringJoiner(CharSequence delimiter, 
             CharSequence prefix, CharSequence suffix)
      Parameters : 
      delimiter - the sequence of characters to be used between
      each element added to the StringJoiner value
      prefix - the sequence of characters to be used at the beginning
      suffix - the sequence of characters to be used at the end
      Throws:
      NullPointerException - if prefix, delimiter, or suffix is null

    方法:在StringJoiner类中有5个方法。

    1. String toString():此方法返回此StringJoiner的String对象。
      Syntax : 
      public String toString()
      Parameters : 
      NA
      Returns :
      the string representation of this StringJoiner
      Overrides :
      toString in class Object
    2. StringJoiner add(CharSequence newElement):此方法将给定的CharSequence值的副本添加为StringJoiner值的下一个元素。如果newElement为null,则添加“null”。
      Syntax : 
      public StringJoiner add(CharSequence newElement)
      Parameters : 
      newElement - The element to add
      Returns :
      a reference to this StringJoiner
    3. StringJoiner merge(StringJoiner other):如果给定的StringJoiner不带前缀和后缀,则将其作为下一个元素添加,如果它不为空。如果给定的StringJoiner为空,则调用不起作用。如果另一个StringJoiner使用不同的分隔符,则来自另一个StringJoiner的元素与该分隔符连接,并且结果作为单个元素附加到此StringJoiner。
      Syntax : 
      public StringJoiner merge(StringJoiner other)
      Parameters : 
      other - The StringJoiner whose contents should be merged
      into this one
      Returns :
      This StringJoiner
      Throws :
      NullPointerException - if the other StringJoiner is null
    4. int length():此方法返回此StringJoiner的字符串表示的长度。
      Syntax : 
      public int length()
      Parameters : 
      NA
      Returns :
      This StringJoiner
    5. StringJoiner setEmptyValue(CharSequence emptyValue):该方法设置要在确定此StringJoiner的字符串表示形式时使用的字符串,并且尚未添加任何元素,即其为空时。
      Syntax : 
      public StringJoiner setEmptyValue(CharSequence emptyValue)
      Parameters : 
      emptyValue - the characters to return as the value 
      of an empty StringJoiner
      Returns :
      this StringJoiner itself so the calls may be chained
      Throws:
      NullPointerException - when the emptyValue parameter is null

    下面是演示所有方法的java程序。

    // Java program to demonstrate methods
    // of StringJoiner class
    ?
    import java.util.ArrayList;
    import java.util.StringJoiner;
    ?????
    public class Test2
    {
    ????public static void main(String[] args)
    ????{
    ???????ArrayList<String> al = new ArrayList<>();
    ????????
    ???????al.add("Ram");
    ???????al.add("Shyam");
    ???????al.add("Alice");
    ???????al.add("Bob");
    ????????
    ???????StringJoiner sj1 = new StringJoiner(",");
    ????????
    ???????// setEmptyValue() method
    ???????sj1.setEmptyValue("sj1 is empty");
    ???????System.out.println(sj1);
    ????????
    ???????// add() method
    ???????sj1.add(al.get(0)).add(al.get(1));
    ???????System.out.println(sj1);
    ????????
    ???????// length() method
    ???????System.out.println("Length of sj1 : " + sj1.length());
    ????????
    ???????StringJoiner sj2 = new StringJoiner(":");
    ???????sj2.add(al.get(2)).add(al.get(3));
    ????????
    ???????//merge() method
    ???????sj1.merge(sj2);
    ????????
    ???????// toString() method
    ???????System.out.println(sj1.toString());
    ????????
    ???????System.out.println("Length of new sj1 : " + sj1.length());
    ?????
    ????}
    }
    

    输出:

    sj1 is empty
    Ram,Shyam
    Length of sj1 : 9
    Ram,Shyam,Alice:Bob
    Length of new sj1 : 19
  • ??Java 字符串
    ]
    转载请保留页面地址:https://www.breakyizhan.com/java/3959.html

    Java8中的Java.util.StringJoiner

    标签:should   port   cal   inter   rgs   character   程序   list   public   

    原文地址:https://www.cnblogs.com/breakyizhan/p/13286155.html

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!