Table of Contents
ToggleConvert Char to String in Java with Examples
使用串接字串、 constructor 、 Apache Commons Lang 等方法進行操作,將 char 轉換為 String, Convert a Char to a String in Java 任務是將該字元轉換為字串,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- conversion
| +- string
| +- CharToStringWithExamplesTest.java
單元測試
Convert a Char to String in Java 提供字元轉換成字串等操作。
concatenation
使用串接字串轉換成字串,若串接 null ,則會傳回 null 字串。
@Test
public void concatenation() {
char value = 'g';
String result = "" + value;
System.out.println(result);
assertEquals("g", result);
result = value + "";
System.out.println(result);
assertEquals("g", result);
String nullValue = null;
result = value + nullValue;
System.out.println(result);
assertEquals("gnull", result);
}
g
g
gnull
constructor
Convert a Char to String in Java 使用 String 建構子,傳入參數字元陣列,建立一個新的 String 物件。
@Test
public void constructor() {
char value = 'g';
String result = new String(new char[] { value });
System.out.println(result);
assertEquals("g", result);
char[] array = new char[] { 'g', 'r', 'a', 'd', 'l', 'e' };
result = new String(array);
System.out.println(result);
assertEquals("gradle", result);
}
g
gradle
constructorThrowException
Convert a Char to String in Java 使用 String 建構子,傳入參數空字元陣列,會拋出例外。
@Test(expected = NullPointerException.class)
public void constructorThrowException() {
char value = 'g';
String result = new String(new char[] { value });
System.out.println(result);
assertEquals("g", result);
char[] array = null;
result = new String(array);
System.out.println(result);
assertEquals("gradle", result);
}
g
java.lang.NullPointerException
at java.lang.String.<init>(String.java:166)
at org.ruoxue.java_147.conversion.CharToStringWithExamplesTest.constructorThrowException(CharToStringWithExamplesTest.java:48)
CharUtils_toString
Convert a Char to String in Java 使用 Apache Commons Lang – CharUtils 轉換成字串。
@Test
public void CharUtils_toString() {
char value = 'g';
String result = CharUtils.toString(value);
System.out.println(result);
assertEquals("g", result);
value = '1';
result = CharUtils.toString(value);
System.out.println(result);
assertEquals("1", result);
}
g
1
CharToStringWithExamplesTest.java
Java Char to String Examples 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.conversion.string;
import static org.junit.Assert.*;
import org.apache.commons.lang3.CharUtils;
import org.junit.Test;
public class CharToStringWithExamplesTest {
@Test
public void concatenation() {
char value = 'g';
String result = "" + value;
System.out.println(result);
assertEquals("g", result);
result = value + "";
System.out.println(result);
assertEquals("g", result);
String nullValue = null;
result = value + nullValue;
System.out.println(result);
assertEquals("gnull", result);
}
@Test
public void constructor() {
char value = 'g';
String result = new String(new char[] { value });
System.out.println(result);
assertEquals("g", result);
char[] array = new char[] { 'g', 'r', 'a', 'd', 'l', 'e' };
result = new String(array);
System.out.println(result);
assertEquals("gradle", result);
}
@Test(expected = NullPointerException.class)
public void constructorThrowException() {
char value = 'g';
String result = new String(new char[] { value });
System.out.println(result);
assertEquals("g", result);
char[] array = null;
result = new String(array);
System.out.println(result);
assertEquals("gradle", result);
}
@Test
public void CharUtils_toString() {
char value = 'g';
String result = CharUtils.toString(value);
System.out.println(result);
assertEquals("g", result);
value = '1';
result = CharUtils.toString(value);
System.out.println(result);
assertEquals("1", result);
}
}
心得分享
Convert a Char to a String in Java with Examples 將 char 基本型別轉換為 String 實例的多種方法 , 也有其他選擇使用外部程式庫來轉換成 String ,像是 Apache Commons Lang 等, Java Char to String Examples 提供了幾種常見方法的操作範例。