<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java 8 `StringJoiner` > 原文: [https://beginnersbook.com/2017/10/java-8-stringjoiner/](https://beginnersbook.com/2017/10/java-8-stringjoiner/) 在 java 8 中,`java.util`包中引入了一個新類`StringJoiner`。使用這個類,我們可以使用指定的分隔符連接多個字符串,我們還可以在連接多個字符串時為最終字符串提供前綴和后綴。在本教程中,我們將看到幾個`StringJoiner`類的示例,在本指南的最后,我們將看到`StringJoiner`類的方法。 ## Java `StringJoiner`示例 1:通過指定分隔符來連接字符串 在這個例子中,我們使用`StringJoiner`連接多個字符串。在創建`StringJoiner`的實例時,我們將分隔符指定為連字符(`-`)。 ```java import java.util.StringJoiner; public class Example { public static void main(String[] args) { // Passing Hyphen(-) as delimiter StringJoiner mystring = new StringJoiner("-"); // Joining multiple strings by using add() method mystring.add("Logan"); mystring.add("Magneto"); mystring.add("Rogue"); mystring.add("Storm"); // Displaying the output String System.out.println(mystring); } } ``` 輸出: ```java Logan-Magneto-Rogue-Storm ``` ## Java `StringJoiner`示例 2:為輸出字符串添加前綴和后綴 ```java import java.util.StringJoiner; public class Example { public static void main(String[] args) { /* Passing comma(,) as delimiter and opening bracket * "(" as prefix and closing bracket ")" as suffix */ StringJoiner mystring = new StringJoiner(",", "(", ")"); // Joining multiple strings by using add() method mystring.add("Negan"); mystring.add("Rick"); mystring.add("Maggie"); mystring.add("Daryl"); // Displaying the output String System.out.println(mystring); } } ``` 輸出: ```java (Negan,Rick,Maggie,Daryl) ``` ## `StringJoiner`示例 3:合并兩個`StringJoiner`對象 ```java import java.util.StringJoiner; public class Example { public static void main(String[] args) { /* Passing comma(,) as delimiter and opening bracket * "(" as prefix and closing bracket ")" as suffix */ StringJoiner mystring = new StringJoiner(",", "(", ")"); mystring.add("Negan"); mystring.add("Rick"); mystring.add("Maggie"); mystring.add("Daryl"); System.out.println("First String: "+mystring); /* Passing hyphen(-) as delimiter and string "pre" * as prefix and string "suff" as suffix */ StringJoiner myanotherstring = new StringJoiner("-", "pre", "suff"); myanotherstring.add("Sansa"); myanotherstring.add("Imp"); myanotherstring.add("Jon"); myanotherstring.add("Ned"); System.out.println("Second String: "+myanotherstring); /* Merging both the strings * The important point to note here is that the output string will be * having the delimiter prefix and suffix of the first string (the string * which is calling the merge method of StringJoiner) */ StringJoiner mergedString = mystring.merge(myanotherstring); System.out.println(mergedString); } } ``` 輸出: ```java First String: (Negan,Rick,Maggie,Daryl) Second String: preSansa-Imp-Jon-Nedsuff (Negan,Rick,Maggie,Daryl,Sansa-Imp-Jon-Ned) ``` 在上面的例子中,我們已經看到了`StringJoiner`類的`add()`和`merge()`方法。讓我們看看這個類的其他方法。 ## `StringJoiner`示例:`setEmptyValue()`,`length()`和`toString()`方法 ```java import java.util.StringJoiner; public class Example { public static void main(String[] args) { //Comma(,) as delimiter StringJoiner mystring = new StringJoiner(","); /* Using setEmptyValue() method, we can set the default value * of a StringJoiner instance, so if the StringJoiner is empty * and we print the value of it, this default value will be * displayed */ mystring.setEmptyValue("This is a default String"); /* We have not added any string to StringJoiner yet so * this should display the default value of StringJoiner */ System.out.println("Default String: "+mystring); // Adding strings to StringJoiner mystring.add("Apple"); mystring.add("Banana"); mystring.add("Orange"); mystring.add("Kiwi"); mystring.add("Grapes"); System.out.println(mystring); /* The length() method of StringJoiner class returns the * length of the string (the number of characters in the * StringJoiner instance) */ int length = mystring.length(); System.out.println("Length of the StringJoiner: "+length); /* The toString() method is used for converting a StringJoiner * instance to a String. */ String s = mystring.toString(); System.out.println(s); } } ``` 輸出: ```java Default String: This is a default String Apple,Banana,Orange,Kiwi,Grapes Length of the StringJoiner: 31 Apple,Banana,Orange,Kiwi,Grapes ``` **參考文獻:** [Java 8 - StringJoiner JavaDoc](https://docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看