Move articles out of java-strings part1
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
This module contains articles about strings in Java.
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Converting Java String to Double](https://www.baeldung.com/java-string-to-double)
|
||||
- [Java String equalsIgnoreCase()](https://www.baeldung.com/java-string-equalsignorecase)
|
||||
- [Finding the Difference Between Two Strings](https://www.baeldung.com/java-difference-between-two-strings)
|
||||
- [Counting Words in a String](https://www.baeldung.com/java-word-counting)
|
||||
- More articles: [[<-- prev>]](/java-strings-2)
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
package com.baeldung.string.charArrayToString;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class CharArrayToStringConversionUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenStringConstructor_thenOK() {
|
||||
final char[] charArray = { 'b', 'a', 'e', 'l', 'd', 'u', 'n', 'g' };
|
||||
|
||||
String string = new String(charArray);
|
||||
|
||||
assertThat(string, is("baeldung"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringCopyValueOf_thenOK() {
|
||||
final char[] charArray = { 'b', 'a', 'e', 'l', 'd', 'u', 'n', 'g' };
|
||||
|
||||
String string = String.copyValueOf(charArray);
|
||||
|
||||
assertThat(string, is("baeldung"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringValueOf_thenOK() {
|
||||
final char[] charArray = { 'b', 'a', 'e', 'l', 'd', 'u', 'n', 'g' };
|
||||
|
||||
String string = String.valueOf(charArray);
|
||||
|
||||
assertThat(string, is("baeldung"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringBuilder_thenOK() {
|
||||
final char[][] arrayOfCharArray = { { 'b', 'a' }, { 'e', 'l', 'd', 'u' }, { 'n', 'g' } };
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (char[] subArray : arrayOfCharArray) {
|
||||
sb.append(subArray);
|
||||
}
|
||||
|
||||
assertThat(sb.toString(), is("baeldung"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStreamCollectors_thenOK() {
|
||||
final Character[] charArray = { 'b', 'a', 'e', 'l', 'd', 'u', 'n', 'g' };
|
||||
|
||||
Stream<Character> charStream = Arrays.stream(charArray);
|
||||
String string = charStream.map(String::valueOf).collect(Collectors.joining());
|
||||
|
||||
assertThat(string, is("baeldung"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGoogleCommonBaseJoiners_thenOK() {
|
||||
final Character[] charArray = { 'b', 'a', 'e', 'l', 'd', 'u', 'n', 'g' };
|
||||
|
||||
String string = Joiner.on("|").join(charArray);
|
||||
|
||||
assertThat(string, is("b|a|e|l|d|u|n|g"));
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package com.baeldung.string.todouble;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.ParseException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StringToDoubleConversionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenValidString_WhenParseDouble_ThenResultIsPrimitiveDouble() {
|
||||
assertEquals(1.23, Double.parseDouble("1.23"), 0.000001);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenNullString_WhenParseDouble_ThenNullPointerExceptionIsThrown() {
|
||||
Double.parseDouble(null);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void givenInalidString_WhenParseDouble_ThenNumberFormatExceptionIsThrown() {
|
||||
Double.parseDouble("&");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidString_WhenValueOf_ThenResultIsPrimitiveDouble() {
|
||||
assertEquals(1.23, Double.valueOf("1.23"), 0.000001);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenNullString_WhenValueOf_ThenNullPointerExceptionIsThrown() {
|
||||
Double.valueOf(null);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void givenInalidString_WhenValueOf_ThenNumberFormatExceptionIsThrown() {
|
||||
Double.valueOf("&");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidString_WhenDecimalFormat_ThenResultIsValidDouble() throws ParseException {
|
||||
assertEquals(1.23, new DecimalFormat("#").parse("1.23").doubleValue(), 0.000001);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenNullString_WhenDecimalFormat_ThenNullPointerExceptionIsThrown() throws ParseException {
|
||||
new DecimalFormat("#").parse(null);
|
||||
}
|
||||
|
||||
@Test(expected = ParseException.class)
|
||||
public void givenInvalidString_WhenDecimalFormat_ThenParseExceptionIsThrown() throws ParseException {
|
||||
new DecimalFormat("#").parse("&");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user