Add code examples for Char to String mini-article
This commit is contained in:
parent
fb5cc7dcf9
commit
2f2999e878
53
core-java-8/src/test/java/com/baeldung/CharToStringTest.java
Normal file
53
core-java-8/src/test/java/com/baeldung/CharToStringTest.java
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class CharToStringTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenChar_shouldConvertToString1() throws Exception {
|
||||||
|
final char givenChar = 'x';
|
||||||
|
|
||||||
|
final String result = String.valueOf(givenChar);
|
||||||
|
|
||||||
|
assertThat(result).isEqualTo("x");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenChar_shouldConvertToString2() throws Exception {
|
||||||
|
final char givenChar = 'x';
|
||||||
|
|
||||||
|
final String result = Character.toString(givenChar);
|
||||||
|
|
||||||
|
assertThat(result).isEqualTo("x");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenChar_shouldConvertToString3() throws Exception {
|
||||||
|
final char givenChar = 'x';
|
||||||
|
|
||||||
|
final String result = new Character(givenChar).toString();
|
||||||
|
|
||||||
|
assertThat(result).isEqualTo("x");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenChar_shouldConvertToString4() throws Exception {
|
||||||
|
final char givenChar = 'x';
|
||||||
|
|
||||||
|
final String result = givenChar + "";
|
||||||
|
|
||||||
|
assertThat(result).isEqualTo("x");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenChar_shouldConvertToString5() throws Exception {
|
||||||
|
final char givenChar = 'x';
|
||||||
|
|
||||||
|
final String result = String.format("%c", givenChar);
|
||||||
|
|
||||||
|
assertThat(result).isEqualTo("x");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user