StringJoiner setEmptyValue() 方法在 Java 8 中
Java 8 中 StringJoiner 類 的 setEmptyValue() 方法設定字元序列。這些字元用於確定此 StringJoiner 的字串表示式以及何時為空。這表示尚未新增任何元素。
語法如下
public StringJoiner setEmptyValue(CharSequence emptyVal)
其中, emptyVal 為一個空 StringJoiner 的值返回的字元
要使用 Java 8 中的 StringJoiner,請匯入以下包。
import java.util.StringJoiner;
以下是一個在 Java 中實現 StringJoiner setEmptyValue() 方法的示例
示例
import java.util.StringJoiner; public class Demo { public static void main(String[] args) { StringJoiner strJoiner = new StringJoiner(","); System.out.println("StringJoiner is empty" + strJoiner); System.out.println(strJoiner.setEmptyValue("Empty!")); System.out.println(strJoiner); System.out.println("After Adding elements to the StringJoiner..."); strJoiner.add("John"); strJoiner.add("Tim"); strJoiner.add("Jacob"); strJoiner.add("Kevin"); strJoiner.add("David"); strJoiner.add("Tom"); System.out.println(strJoiner); } }
輸出
StringJoiner is empty Empty! Empty! After Adding elements to the StringJoiner... John,Tim,Jacob,Kevin,David,Tom
廣告