Table of Contents
ToggleJava StringJoiner Methods
用來簡化指定分割符號來拼接字串,並且可以選擇前綴、後綴符號,雖然使用 StringBuilder 在每個字串後附加分割符號,也能夠實現,但 StringJoiner 提供了一種無需撰寫太多程式碼,更加簡便的方式, StringJoiner Java Methods 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- string
| +- stringjoiner
| +- StringJoinerMethodsTest.java
單元測試
提供指定前綴、後綴及分隔符號等拼接字串。
add
建立一個 StringJoiner ,指定分隔符號,拼接字串。
@Test
public void add() {
StringJoiner joiner = new StringJoiner(",");
joiner.add("Java");
joiner.add("Spring");
joiner.add("JUnit");
System.out.println(joiner);
assertEquals("Java,Spring,JUnit", joiner.toString());
}
Java,Spring,JUnit
prefixAndSuffix
建立一個 StringJoiner ,指定前綴、後綴及分隔符號,拼接字串。
@Test
public void prefixAndSuffix() {
StringJoiner joiner = new StringJoiner(",", "[", "]");
joiner.add("Java");
joiner.add("Spring");
joiner.add("JUnit");
System.out.println(joiner);
assertEquals("[Java,Spring,JUnit]", joiner.toString());
}
[Java,Spring,JUnit]
merge
StringJoiner Methods in Java 建立兩個 StringJoiner ,指定前綴、後綴及分隔符號,使用 merge 拼接字串。
@Test
public void merge() {
StringJoiner joiner = new StringJoiner(",", "[", "]");
joiner.add("Java");
joiner.add("Spring");
StringJoiner joiner2 = new StringJoiner("-", "[", "]");
joiner2.add("JUnit");
joiner2.add("Assertj");
joiner.merge(joiner2);
System.out.println(joiner);
assertEquals("[Java,Spring,JUnit-Assertj]", joiner.toString());
}
[Java,Spring,JUnit-Assertj]
length
StringJoiner Methods in Java 建立一個 StringJoiner ,增加字串,取得長度。
@Test
public void length() {
int expectedSize = 17;
StringJoiner joiner = new StringJoiner(",");
joiner.add("Java");
joiner.add("Spring");
joiner.add("JUnit");
System.out.println(joiner.length());
assertEquals(expectedSize, joiner.length());
}
17
setEmptyValue
StringJoiner Methods in Java 建立一個 StringJoiner ,預設空值。
@Test
public void setEmptyValue() {
StringJoiner joiner = new StringJoiner(",");
joiner.setEmptyValue("default");
System.out.println(joiner);
assertEquals("default", joiner.toString());
}
default
String_join
StringJoiner Methods Java 使用 String join ,指定分隔符號,拼接字串。
@Test
public void String_join() {
String[] array = { "Java", "Spring", "JUnit" };
String result = String.join(",", array);
System.out.println(result);
Java,Spring,JUnit
Collectors_joining
StringJoiner Methods Java 使用 Collectors joining ,指定前綴、後綴及分隔符號,拼接字串。
@Test
public void Collectors_joining() {
List<String> list = Arrays.asList("Java", "Spring", "JUnit");
String result = list.stream().map(e -> e.toString()).collect(Collectors.joining(","));
System.out.println(result);
assertEquals("Java,Spring,JUnit", result);
result = list.stream().map(e -> e.toString()).collect(Collectors.joining(",", "[", "]"));
System.out.println(result);
assertEquals("[Java,Spring,JUnit]", result);
}
Java,Spring,JUnit
[Java,Spring,JUnit]
StringJoinerMethodsTest.java
StringJoiner Methods in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.string.stringjoiner;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;
import org.junit.Test;
public class StringJoinerMethodsTest {
@Test
public void add() {
StringJoiner joiner = new StringJoiner(",");
joiner.add("Java");
joiner.add("Spring");
joiner.add("JUnit");
System.out.println(joiner);
assertEquals("Java,Spring,JUnit", joiner.toString());
}
@Test
public void prefixAndSuffix() {
StringJoiner joiner = new StringJoiner(",", "[", "]");
joiner.add("Java");
joiner.add("Spring");
joiner.add("JUnit");
System.out.println(joiner);
assertEquals("[Java,Spring,JUnit]", joiner.toString());
}
@Test
public void merge() {
StringJoiner joiner = new StringJoiner(",", "[", "]");
joiner.add("Java");
joiner.add("Spring");
StringJoiner joiner2 = new StringJoiner("-", "[", "]");
joiner2.add("JUnit");
joiner2.add("Assertj");
joiner.merge(joiner2);
System.out.println(joiner);
assertEquals("[Java,Spring,JUnit-Assertj]", joiner.toString());
}
@Test
public void length() {
int expectedSize = 17;
StringJoiner joiner = new StringJoiner(",");
joiner.add("Java");
joiner.add("Spring");
joiner.add("JUnit");
System.out.println(joiner.length());
assertEquals(expectedSize, joiner.length());
}
@Test
public void setEmptyValue() {
StringJoiner joiner = new StringJoiner(",");
joiner.setEmptyValue("default");
System.out.println(joiner);
assertEquals("default", joiner.toString());
}
@Test
public void String_join() {
String[] array = { "Java", "Spring", "JUnit" };
String result = String.join(",", array);
System.out.println(result);
assertEquals("Java,Spring,JUnit", result);
}
@Test
public void Collectors_joining() {
List<String> list = Arrays.asList("Java", "Spring", "JUnit");
String result = list.stream().map(e -> e.toString()).collect(Collectors.joining(","));
System.out.println(result);
assertEquals("Java,Spring,JUnit", result);
result = list.stream().map(e -> e.toString()).collect(Collectors.joining(",", "[", "]"));
System.out.println(result);
assertEquals("[Java,Spring,JUnit]", result);
}
}
心得分享
StringJoiner Functions in Java 屬於 java.util,用於指定前綴、後綴及分隔符號,拼接字串,熟悉 StringJoiner Methods in Java 這些方法的操作,提升開發效率,在應用上相當廣泛。