Table of Contents
ToggleInitialize String in Java
一般而言有幾種字串初始化的方法,除了使用文字或 new 關鍵字建立字串之外,String 也提供了 int[] 、 StringBuffer 、 StringBuilder 等建構子來建立字串, Initialize String Java 介紹了初始化字串等操作,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- string
| +- InitializeStringTest.java
單元測試
Java Initialize String 提供初始化建立字串。
withIntArray
使用 int 陣列,建立一個 String 。
@Test
public void withIntArray() {
int[] value = new int[] { 65, 115, 115, 101, 114, 116, 106 };
String result = new String(value, 0, value.length);
System.out.println(result);
assertEquals("Assertj", result);
int[] value2 = new int[] { 65, 115, 115, 101, 114, 116, 106, 32, 49, 53, 53 };
String result2 = new String(value2, 8, 3);
System.out.println(result2);
assertEquals("155", result2);
}
Assertj
155
withString
Java Initialize String 使用另一個字串,建立一個 String 。
@Test
public void withString() {
String value = "Assertj";
String result = new String(value);
System.out.println(result);
assertEquals("Assertj", result);
String value2 = "155";
String result2 = new String(value2);
System.out.println(result2);
assertEquals("155", result2);
}
Assertj
155
withStringBuffer
Java Initialize String 使用 StringBuffer ,建立一個 String 。
@Test
public void withStringBuffer() {
StringBuffer value = new StringBuffer("Assertj");
String result = new String(value);
System.out.println(result);
assertEquals("Assertj", result);
value.append(" 155");
String result2 = new String(value);
System.out.println(result2);
assertEquals("Assertj 155", result2);
}
Assertj
Assertj 155
withStringBuilder
Java Initialize String 使用 StringBuilder ,建立一個 String 。
@Test
public void withStringBuilder() {
StringBuilder value = new StringBuilder("Assertj");
String result = new String(value);
System.out.println(result);
assertEquals("Assertj", result);
value.append(" 155");
String result2 = new String(value);
System.out.println(result2);
assertEquals("Assertj 155", result2);
}
Assertj
Assertj 155
InitializeStringTest.java
String Initialization Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.string;
import static org.junit.Assert.*;
import org.junit.Test;
public class InitializeStringTest {
@Test
public void withIntArray() {
int[] value = new int[] { 65, 115, 115, 101, 114, 116, 106 };
String result = new String(value, 0, value.length);
System.out.println(result);
assertEquals("Assertj", result);
int[] value2 = new int[] { 65, 115, 115, 101, 114, 116, 106, 32, 49, 53, 53 };
String result2 = new String(value2, 8, 3);
System.out.println(result2);
assertEquals("155", result2);
}
@Test
public void withString() {
String value = "Assertj";
String result = new String(value);
System.out.println(result);
assertEquals("Assertj", result);
String value2 = "155";
String result2 = new String(value2);
System.out.println(result2);
assertEquals("155", result2);
}
@Test
public void withStringBuffer() {
StringBuffer value = new StringBuffer("Assertj");
String result = new String(value);
System.out.println(result);
assertEquals("Assertj", result);
value.append(" 155");
String result2 = new String(value);
System.out.println(result2);
assertEquals("Assertj 155", result2);
}
@Test
public void withStringBuilder() {
StringBuilder value = new StringBuilder("Assertj");
String result = new String(value);
System.out.println(result);
assertEquals("Assertj", result);
value.append(" 155");
String result2 = new String(value);
System.out.println(result2);
assertEquals("Assertj 155", result2);
}
}
心得分享
Java String Initialize 初始化 String,在 Java 中,字串是不可變的物件,一旦建立後就不能改變,String Initialization Java 提供了幾種 String 初始化的操作範例,使用單元測試驗證產出結果。