[BAEL-8232] - Moved java string related code and github links into new module java-strings
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class CharSequenceVsStringUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenUsingString_whenInstantiatingString_thenWrong() {
|
||||
CharSequence firstString = "bealdung";
|
||||
String secondString = "baeldung";
|
||||
|
||||
assertNotEquals(firstString, secondString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIdenticalCharSequences_whenCastToString_thenEqual() {
|
||||
CharSequence charSequence1 = "baeldung_1";
|
||||
CharSequence charSequence2 = "baeldung_2";
|
||||
|
||||
assertTrue(charSequence1.toString().compareTo(charSequence2.toString()) < 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenAppended_thenUnmodified() {
|
||||
String test = "a";
|
||||
int firstAddressOfTest = System.identityHashCode(test);
|
||||
test += "b";
|
||||
int secondAddressOfTest = System.identityHashCode(test);
|
||||
|
||||
assertNotEquals(firstAddressOfTest, secondAddressOfTest);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringBuilder_whenAppended_thenModified() {
|
||||
StringBuilder test = new StringBuilder();
|
||||
test.append("a");
|
||||
int firstAddressOfTest = System.identityHashCode(test);
|
||||
test.append("b");
|
||||
int secondAddressOfTest = System.identityHashCode(test);
|
||||
|
||||
assertEquals(firstAddressOfTest, secondAddressOfTest);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class JoinerSplitterUnitTest {
|
||||
|
||||
@Test
|
||||
public void provided_array_convert_to_stream_and_convert_to_string() {
|
||||
|
||||
String[] programming_languages = {"java", "python", "nodejs", "ruby"};
|
||||
|
||||
String expectation = "java,python,nodejs,ruby";
|
||||
|
||||
String result = JoinerSplitter.join(programming_languages);
|
||||
assertEquals(result, expectation);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArray_transformedToStream_convertToPrefixPostfixString() {
|
||||
|
||||
String[] programming_languages = {"java", "python",
|
||||
"nodejs", "ruby"};
|
||||
String expectation = "[java,python,nodejs,ruby]";
|
||||
|
||||
String result = JoinerSplitter.joinWithPrefixPostFix(programming_languages);
|
||||
assertEquals(result, expectation);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_transformedToStream_convertToList() {
|
||||
|
||||
String programming_languages = "java,python,nodejs,ruby";
|
||||
|
||||
List<String> expectation = new ArrayList<String>();
|
||||
expectation.add("java");
|
||||
expectation.add("python");
|
||||
expectation.add("nodejs");
|
||||
expectation.add("ruby");
|
||||
|
||||
List<String> result = JoinerSplitter.split(programming_languages);
|
||||
|
||||
assertEquals(result, expectation);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_transformedToStream_convertToListOfChar() {
|
||||
|
||||
String programming_languages = "java,python,nodejs,ruby";
|
||||
|
||||
List<Character> expectation = new ArrayList<Character>();
|
||||
char[] charArray = programming_languages.toCharArray();
|
||||
for (char c : charArray) {
|
||||
expectation.add(c);
|
||||
}
|
||||
|
||||
List<Character> result = JoinerSplitter.splitToListOfChar(programming_languages);
|
||||
assertEquals(result, expectation);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PalindromeUnitTest {
|
||||
|
||||
private String[] words = {
|
||||
"Anna",
|
||||
"Civic",
|
||||
"Kayak",
|
||||
"Level",
|
||||
"Madam",
|
||||
};
|
||||
|
||||
private String[] sentences = {
|
||||
"Sore was I ere I saw Eros",
|
||||
"Euston saw I was not Sue",
|
||||
"Too hot to hoot",
|
||||
"No mists or frost Simon",
|
||||
"Stella won no wallets"
|
||||
};
|
||||
|
||||
private Palindrome palindrome = new Palindrome();
|
||||
|
||||
@Test
|
||||
public void whenWord_shouldBePalindrome() {
|
||||
for (String word : words)
|
||||
assertTrue(palindrome.isPalindrome(word));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSentence_shouldBePalindrome() {
|
||||
for (String sentence : sentences)
|
||||
assertTrue(palindrome.isPalindrome(sentence));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReverseWord_shouldBePalindrome() {
|
||||
for (String word : words)
|
||||
assertTrue(palindrome.isPalindromeReverseTheString(word));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReverseSentence_shouldBePalindrome() {
|
||||
for (String sentence : sentences)
|
||||
assertTrue(palindrome.isPalindromeReverseTheString(sentence));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringBuilderWord_shouldBePalindrome() {
|
||||
for (String word : words)
|
||||
assertTrue(palindrome.isPalindromeUsingStringBuilder(word));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringBuilderSentence_shouldBePalindrome() {
|
||||
for (String sentence : sentences)
|
||||
assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringBufferWord_shouldBePalindrome() {
|
||||
for (String word : words)
|
||||
assertTrue(palindrome.isPalindromeUsingStringBuffer(word));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringBufferSentence_shouldBePalindrome() {
|
||||
for (String sentence : sentences)
|
||||
assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPalindromeRecursive_wordShouldBePalindrome() {
|
||||
for (String word : words)
|
||||
assertTrue(palindrome.isPalindromeRecursive(word));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPalindromeRecursive_sentenceShouldBePalindrome() {
|
||||
for (String sentence : sentences)
|
||||
assertTrue(palindrome.isPalindromeRecursive(sentence));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPalindromeStreams_wordShouldBePalindrome() {
|
||||
for (String word : words)
|
||||
assertTrue(palindrome.isPalindromeUsingIntStream(word));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPalindromeStreams_sentenceShouldBePalindrome() {
|
||||
for (String sentence : sentences)
|
||||
assertTrue(palindrome.isPalindromeUsingIntStream(sentence));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
|
||||
public class SplitUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenString_whenSplit_thenReturnsArray_through_JavaLangString() {
|
||||
assertThat("peter,james,thomas".split(","))
|
||||
.containsExactly("peter", "james", "thomas");
|
||||
|
||||
assertThat("car jeep scooter".split(" "))
|
||||
.containsExactly("car", "jeep", "scooter");
|
||||
|
||||
assertThat("1-120-232323".split("-"))
|
||||
.containsExactly("1", "120", "232323");
|
||||
|
||||
assertThat("192.168.1.178".split("\\."))
|
||||
.containsExactly("192", "168", "1", "178");
|
||||
|
||||
assertThat("b a, e, l.d u, n g".split("\\s+|,\\s*|\\.\\s*"))
|
||||
.containsExactly("b", "a", "e", "l", "d", "u", "n", "g");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenSplit_thenReturnsArray_through_StringUtils() {
|
||||
StringUtils.split("car jeep scooter");
|
||||
|
||||
assertThat(StringUtils.split("car jeep scooter"))
|
||||
.containsExactly("car", "jeep", "scooter");
|
||||
|
||||
assertThat(StringUtils.split("car jeep scooter"))
|
||||
.containsExactly("car", "jeep", "scooter");
|
||||
|
||||
assertThat(StringUtils.split("car:jeep:scooter", ":"))
|
||||
.containsExactly("car", "jeep", "scooter");
|
||||
|
||||
assertThat(StringUtils.split("car.jeep.scooter", "."))
|
||||
.containsExactly("car", "jeep", "scooter");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenSplit_thenReturnsList_Splitter() {
|
||||
//given
|
||||
List<String> resultList = Splitter.on(',').trimResults().omitEmptyStrings().splitToList("car,jeep,, scooter");
|
||||
|
||||
assertThat(resultList)
|
||||
.containsExactly("car", "jeep", "scooter");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class StringComparisonUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingComparisonOperator_ThenComparingStrings(){
|
||||
|
||||
String string1 = "using comparison operator";
|
||||
String string2 = "using comparison operator";
|
||||
String string3 = new String("using comparison operator");
|
||||
|
||||
assertThat(string1 == string2).isTrue();
|
||||
assertThat(string1 == string3).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEqualsMethod_ThenComparingStrings(){
|
||||
|
||||
String string1 = "using equals method";
|
||||
String string2 = "using equals method";
|
||||
|
||||
String string3 = "using EQUALS method";
|
||||
String string4 = new String("using equals method");
|
||||
|
||||
assertThat(string1.equals(string2)).isTrue();
|
||||
assertThat(string1.equals(string4)).isTrue();
|
||||
|
||||
assertThat(string1.equals(null)).isFalse();
|
||||
assertThat(string1.equals(string3)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEqualsIgnoreCase_ThenComparingStrings(){
|
||||
|
||||
String string1 = "using equals ignore case";
|
||||
String string2 = "USING EQUALS IGNORE CASE";
|
||||
|
||||
assertThat(string1.equalsIgnoreCase(string2)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingCompareTo_ThenComparingStrings(){
|
||||
|
||||
String author = "author";
|
||||
String book = "book";
|
||||
String duplicateBook = "book";
|
||||
|
||||
assertThat(author.compareTo(book)).isEqualTo(-1);
|
||||
assertThat(book.compareTo(author)).isEqualTo(1);
|
||||
assertThat(duplicateBook.compareTo(book)).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingCompareToIgnoreCase_ThenComparingStrings(){
|
||||
|
||||
String author = "Author";
|
||||
String book = "book";
|
||||
String duplicateBook = "BOOK";
|
||||
|
||||
assertThat(author.compareToIgnoreCase(book)).isEqualTo(-1);
|
||||
assertThat(book.compareToIgnoreCase(author)).isEqualTo(1);
|
||||
assertThat(duplicateBook.compareToIgnoreCase(book)).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingObjectsEqualsMethod_ThenComparingStrings(){
|
||||
|
||||
String string1 = "using objects equals";
|
||||
String string2 = "using objects equals";
|
||||
String string3 = new String("using objects equals");
|
||||
|
||||
assertThat(Objects.equals(string1, string2)).isTrue();
|
||||
assertThat(Objects.equals(string1, string3)).isTrue();
|
||||
|
||||
assertThat(Objects.equals(null, null)).isTrue();
|
||||
assertThat(Objects.equals(null, string1)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEqualsOfApacheCommons_ThenComparingStrings(){
|
||||
|
||||
assertThat(StringUtils.equals(null, null)).isTrue();
|
||||
assertThat(StringUtils.equals(null, "equals method")).isFalse();
|
||||
assertThat(StringUtils.equals("equals method", "equals method")).isTrue();
|
||||
assertThat(StringUtils.equals("equals method", "EQUALS METHOD")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEqualsIgnoreCaseOfApacheCommons_ThenComparingStrings(){
|
||||
|
||||
assertThat(StringUtils.equalsIgnoreCase(null, null)).isTrue();
|
||||
assertThat(StringUtils.equalsIgnoreCase(null, "equals method")).isFalse();
|
||||
assertThat(StringUtils.equalsIgnoreCase("equals method", "equals method")).isTrue();
|
||||
assertThat(StringUtils.equalsIgnoreCase("equals method", "EQUALS METHOD")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEqualsAnyOf_ThenComparingStrings(){
|
||||
|
||||
assertThat(StringUtils.equalsAny(null, null, null)).isTrue();
|
||||
assertThat(StringUtils.equalsAny("equals any", "equals any", "any")).isTrue();
|
||||
assertThat(StringUtils.equalsAny("equals any", null, "equals any")).isTrue();
|
||||
assertThat(StringUtils.equalsAny(null, "equals", "any")).isFalse();
|
||||
assertThat(StringUtils.equalsAny("equals any", "EQUALS ANY", "ANY")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEqualsAnyIgnoreCase_ThenComparingStrings(){
|
||||
|
||||
assertThat(StringUtils.equalsAnyIgnoreCase(null, null, null)).isTrue();
|
||||
assertThat(StringUtils.equalsAnyIgnoreCase("equals any", "equals any", "any")).isTrue();
|
||||
assertThat(StringUtils.equalsAnyIgnoreCase("equals any", null, "equals any")).isTrue();
|
||||
assertThat(StringUtils.equalsAnyIgnoreCase(null, "equals", "any")).isFalse();
|
||||
assertThat(StringUtils.equalsAnyIgnoreCase(
|
||||
"equals any ignore case", "EQUALS ANY IGNORE CASE", "any")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingCompare_thenComparingStringsWithNulls(){
|
||||
|
||||
assertThat(StringUtils.compare(null, null)).isEqualTo(0);
|
||||
assertThat(StringUtils.compare(null, "abc")).isEqualTo(-1);
|
||||
|
||||
assertThat(StringUtils.compare("abc", "bbc")).isEqualTo(-1);
|
||||
assertThat(StringUtils.compare("bbc", "abc")).isEqualTo(1);
|
||||
assertThat(StringUtils.compare("abc", "abc")).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingCompareIgnoreCase_ThenComparingStringsWithNulls(){
|
||||
|
||||
assertThat(StringUtils.compareIgnoreCase(null, null)).isEqualTo(0);
|
||||
assertThat(StringUtils.compareIgnoreCase(null, "abc")).isEqualTo(-1);
|
||||
|
||||
assertThat(StringUtils.compareIgnoreCase("Abc", "bbc")).isEqualTo(-1);
|
||||
assertThat(StringUtils.compareIgnoreCase("bbc", "ABC")).isEqualTo(1);
|
||||
assertThat(StringUtils.compareIgnoreCase("abc", "ABC")).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingCompareWithNullIsLessOption_ThenComparingStrings(){
|
||||
|
||||
assertThat(StringUtils.compare(null, "abc", true)).isEqualTo(-1);
|
||||
assertThat(StringUtils.compare(null, "abc", false)).isEqualTo(1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
public class StringHelperUnitTest {
|
||||
|
||||
public static final String TEST_STRING = "abcdef";
|
||||
public static final String NULL_STRING = null;
|
||||
public static final String EMPTY_STRING = "";
|
||||
public static final String ONE_CHAR_STRING = "a";
|
||||
public static final String WHITE_SPACE_AT_THE_END_STRING = "abc ";
|
||||
public static final String NEW_LINE_AT_THE_END_STRING = "abc\n";
|
||||
public static final String MULTIPLE_LINES_STRING = "abc\ndef";
|
||||
|
||||
@Test
|
||||
public void givenTestString_whenSubstring_thenGetStingWithoutLastChar() {
|
||||
assertEquals("abcde", StringHelper.removeLastChar(TEST_STRING));
|
||||
assertEquals("abcde", StringUtils.substring(TEST_STRING, 0, TEST_STRING.length() - 1));
|
||||
assertEquals("abcde", StringUtils.chop(TEST_STRING));
|
||||
assertEquals("abcde", TEST_STRING.replaceAll(".$", ""));
|
||||
assertEquals("abcde", StringHelper.removeLastCharRegex(TEST_STRING));
|
||||
assertEquals("abcde", StringHelper.removeLastCharOptional(TEST_STRING));
|
||||
assertEquals("abcde", StringHelper.removeLastCharRegexOptional(TEST_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNullString_whenSubstring_thenGetNullString() {
|
||||
assertEquals(NULL_STRING, StringHelper.removeLastChar(NULL_STRING));
|
||||
assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING));
|
||||
assertEquals(NULL_STRING, StringHelper.removeLastCharRegex(NULL_STRING));
|
||||
assertEquals(NULL_STRING, StringHelper.removeLastCharOptional(NULL_STRING));
|
||||
assertEquals(NULL_STRING, StringHelper.removeLastCharRegexOptional(NULL_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyString_whenSubstring_thenGetEmptyString() {
|
||||
assertEquals(EMPTY_STRING, StringHelper.removeLastChar(EMPTY_STRING));
|
||||
assertEquals(EMPTY_STRING, StringUtils.substring(EMPTY_STRING, 0, EMPTY_STRING.length() - 1));
|
||||
assertEquals(EMPTY_STRING, StringUtils.chop(EMPTY_STRING));
|
||||
assertEquals(EMPTY_STRING, EMPTY_STRING.replaceAll(".$", ""));
|
||||
assertEquals(EMPTY_STRING, StringHelper.removeLastCharRegex(EMPTY_STRING));
|
||||
assertEquals(EMPTY_STRING, StringHelper.removeLastCharOptional(EMPTY_STRING));
|
||||
assertEquals(EMPTY_STRING, StringHelper.removeLastCharRegexOptional(EMPTY_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOneCharString_whenSubstring_thenGetEmptyString() {
|
||||
assertEquals(EMPTY_STRING, StringHelper.removeLastChar(ONE_CHAR_STRING));
|
||||
assertEquals(EMPTY_STRING, StringUtils.substring(ONE_CHAR_STRING, 0, ONE_CHAR_STRING.length() - 1));
|
||||
assertEquals(EMPTY_STRING, StringUtils.chop(ONE_CHAR_STRING));
|
||||
assertEquals(EMPTY_STRING, ONE_CHAR_STRING.replaceAll(".$", ""));
|
||||
assertEquals(EMPTY_STRING, StringHelper.removeLastCharRegex(ONE_CHAR_STRING));
|
||||
assertEquals(EMPTY_STRING, StringHelper.removeLastCharOptional(ONE_CHAR_STRING));
|
||||
assertEquals(EMPTY_STRING, StringHelper.removeLastCharRegexOptional(ONE_CHAR_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringWithWhiteSpaceAtTheEnd_whenSubstring_thenGetStringWithoutWhiteSpaceAtTheEnd() {
|
||||
assertEquals("abc", StringHelper.removeLastChar(WHITE_SPACE_AT_THE_END_STRING));
|
||||
assertEquals("abc", StringUtils.substring(WHITE_SPACE_AT_THE_END_STRING, 0, WHITE_SPACE_AT_THE_END_STRING.length() - 1));
|
||||
assertEquals("abc", StringUtils.chop(WHITE_SPACE_AT_THE_END_STRING));
|
||||
assertEquals("abc", WHITE_SPACE_AT_THE_END_STRING.replaceAll(".$", ""));
|
||||
assertEquals("abc", StringHelper.removeLastCharRegex(WHITE_SPACE_AT_THE_END_STRING));
|
||||
assertEquals("abc", StringHelper.removeLastCharOptional(WHITE_SPACE_AT_THE_END_STRING));
|
||||
assertEquals("abc", StringHelper.removeLastCharRegexOptional(WHITE_SPACE_AT_THE_END_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringWithNewLineAtTheEnd_whenSubstring_thenGetStringWithoutNewLine() {
|
||||
assertEquals("abc", StringHelper.removeLastChar(NEW_LINE_AT_THE_END_STRING));
|
||||
assertEquals("abc", StringUtils.substring(NEW_LINE_AT_THE_END_STRING, 0, NEW_LINE_AT_THE_END_STRING.length() - 1));
|
||||
assertEquals("abc", StringUtils.chop(NEW_LINE_AT_THE_END_STRING));
|
||||
assertNotEquals("abc", NEW_LINE_AT_THE_END_STRING.replaceAll(".$", ""));
|
||||
assertNotEquals("abc", StringHelper.removeLastCharRegex(NEW_LINE_AT_THE_END_STRING));
|
||||
assertEquals("abc", StringHelper.removeLastCharOptional(NEW_LINE_AT_THE_END_STRING));
|
||||
assertNotEquals("abc", StringHelper.removeLastCharRegexOptional(NEW_LINE_AT_THE_END_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiLineString_whenSubstring_thenGetStringWithoutNewLine() {
|
||||
assertEquals("abc\nde", StringHelper.removeLastChar(MULTIPLE_LINES_STRING));
|
||||
assertEquals("abc\nde", StringUtils.substring(MULTIPLE_LINES_STRING, 0, MULTIPLE_LINES_STRING.length() - 1));
|
||||
assertEquals("abc\nde", StringUtils.chop(MULTIPLE_LINES_STRING));
|
||||
assertEquals("abc\nde", MULTIPLE_LINES_STRING.replaceAll(".$", ""));
|
||||
assertEquals("abc\nde", StringHelper.removeLastCharRegex(MULTIPLE_LINES_STRING));
|
||||
assertEquals("abc\nde", StringHelper.removeLastCharOptional(MULTIPLE_LINES_STRING));
|
||||
assertEquals("abc\nde", StringHelper.removeLastCharRegexOptional(MULTIPLE_LINES_STRING));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class StringToCharStreamUnitTest {
|
||||
|
||||
private String testString = "Tests";
|
||||
|
||||
@Test
|
||||
public void givenTestString_whenChars_thenReturnIntStream() {
|
||||
assertThat(testString.chars(), instanceOf(IntStream.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestString_whenCodePoints_thenReturnIntStream() {
|
||||
assertThat(testString.codePoints(), instanceOf(IntStream.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestString_whenCodePoints_thenShowOccurences() throws Exception {
|
||||
Map<Character, Integer> map = testString.codePoints()
|
||||
.mapToObj(c -> (char) c)
|
||||
.filter(Character::isLetter)
|
||||
.collect(Collectors.toMap(c -> c, c -> 1, Integer::sum));
|
||||
|
||||
System.out.println(map);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntStream_whenMapToObj_thenReturnCharacterStream() {
|
||||
Stream<Character> characterStream = testString.chars()
|
||||
.mapToObj(c -> (char) c);
|
||||
Stream<Character> characterStream1 = testString.codePoints()
|
||||
.mapToObj(c -> (char) c);
|
||||
assertNotNull("IntStream returned by chars() did not map to Stream<Character>", characterStream);
|
||||
assertNotNull("IntStream returned by codePoints() did not map to Stream<Character>", characterStream1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntStream_whenMapToObj_thenReturnStringStream() {
|
||||
List<String> strings = testString.codePoints()
|
||||
.mapToObj(c -> String.valueOf((char) c))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(strings.size(), 5);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.IllegalFormatException;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StringUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCallCodePointAt_thenDecimalUnicodeReturned() {
|
||||
assertEquals(97, "abcd".codePointAt(0));
|
||||
}
|
||||
|
||||
@Test(expected = StringIndexOutOfBoundsException.class)
|
||||
public void whenPassNonExistingIndex_thenExceptionThrown() {
|
||||
int a = "abcd".codePointAt(4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallConcat_thenCorrect() {
|
||||
assertEquals("elephant", "elep".concat("hant"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetBytes_thenCorrect() throws UnsupportedEncodingException {
|
||||
byte[] byteArray1 = "abcd".getBytes();
|
||||
byte[] byteArray2 = "efgh".getBytes(StandardCharsets.US_ASCII);
|
||||
byte[] byteArray3 = "ijkl".getBytes("UTF-8");
|
||||
byte[] expected1 = new byte[] { 97, 98, 99, 100 };
|
||||
byte[] expected2 = new byte[] { 101, 102, 103, 104 };
|
||||
byte[] expected3 = new byte[] { 105, 106, 107, 108 };
|
||||
|
||||
assertArrayEquals(expected1, byteArray1);
|
||||
assertArrayEquals(expected2, byteArray2);
|
||||
assertArrayEquals(expected3, byteArray3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetBytesUsingASCII_thenCorrect() {
|
||||
byte[] byteArray = "efgh".getBytes(StandardCharsets.US_ASCII);
|
||||
byte[] expected = new byte[] { 101, 102, 103, 104 };
|
||||
|
||||
assertArrayEquals(expected, byteArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreateStringUsingByteArray_thenCorrect() {
|
||||
byte[] array = new byte[] { 97, 98, 99, 100 };
|
||||
String s = new String(array);
|
||||
|
||||
assertEquals("abcd", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallCharAt_thenCorrect() {
|
||||
assertEquals('P', "Paul".charAt(0));
|
||||
}
|
||||
|
||||
@Test(expected = IndexOutOfBoundsException.class)
|
||||
public void whenCharAtOnNonExistingIndex_thenIndexOutOfBoundsExceptionThrown() {
|
||||
char character = "Paul".charAt(4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallCodePointCount_thenCorrect() {
|
||||
assertEquals(2, "abcd".codePointCount(0, 2));
|
||||
}
|
||||
|
||||
@Test(expected = IndexOutOfBoundsException.class)
|
||||
public void whenSecondIndexEqualToLengthOfString_thenIndexOutOfBoundsExceptionThrown() {
|
||||
char character = "Paul".charAt(4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallContains_thenCorrect() {
|
||||
String s = "abcd";
|
||||
|
||||
assertTrue(s.contains("abc"));
|
||||
assertFalse(s.contains("cde"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallCopyValueOf_thenStringConstructed() {
|
||||
char[] array = new char[] { 'a', 'b', 'c', 'd' };
|
||||
|
||||
assertEquals("abcd", String.copyValueOf(array));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallEndsWith_thenCorrect() {
|
||||
String s1 = "test";
|
||||
|
||||
assertTrue(s1.endsWith("t"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFormat_thenCorrect() {
|
||||
String value = "Baeldung";
|
||||
String formatted = String.format("Welcome to %s!", value);
|
||||
|
||||
assertEquals("Welcome to Baeldung!", formatted);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalFormatException.class)
|
||||
public void whenInvalidFormatSyntax_thenIllegalFormatExceptionThrown() {
|
||||
String value = "Baeldung";
|
||||
String formatted = String.format("Welcome to %x!", value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallIndexOf_thenCorrect() {
|
||||
assertEquals(1, "foo".indexOf("o"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallIsEmpty_thenCorrect() {
|
||||
String s1 = "";
|
||||
|
||||
assertTrue(s1.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallLastIndexOf_thenCorrect() {
|
||||
assertEquals(2, "foo".lastIndexOf("o"));
|
||||
assertEquals(2, "foo".lastIndexOf(111));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallRegionMatches_thenCorrect() {
|
||||
assertTrue("welcome to baeldung".regionMatches(false, 11, "baeldung", 0, 8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallStartsWith_thenCorrect() {
|
||||
assertTrue("foo".startsWith("f"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTrim_thenCorrect() {
|
||||
assertEquals("foo", " foo ".trim());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSplit_thenCorrect() {
|
||||
String s = "Welcome to Baeldung";
|
||||
String[] array = new String[] { "Welcome", "to", "Baeldung" };
|
||||
|
||||
assertArrayEquals(array, s.split(" "));
|
||||
}
|
||||
|
||||
@Test(expected = PatternSyntaxException.class)
|
||||
public void whenPassInvalidParameterToSplit_thenPatternSyntaxExceptionThrown() {
|
||||
String s = "Welcome*to Baeldung";
|
||||
|
||||
String[] result = s.split("*");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallSubSequence_thenCorrect() {
|
||||
String s = "Welcome to Baeldung";
|
||||
|
||||
assertEquals("Welcome", s.subSequence(0, 7));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallSubstring_thenCorrect() {
|
||||
String s = "Welcome to Baeldung";
|
||||
|
||||
assertEquals("Welcome", s.substring(0, 7));
|
||||
}
|
||||
|
||||
@Test(expected = IndexOutOfBoundsException.class)
|
||||
public void whenSecondIndexEqualToLengthOfString_thenCorrect() {
|
||||
String s = "Welcome to Baeldung";
|
||||
|
||||
String sub = s.substring(0, 20);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertToUpperCase_thenCorrect() {
|
||||
String s = "Welcome to Baeldung!";
|
||||
|
||||
assertEquals("WELCOME TO BAELDUNG!", s.toUpperCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertToLowerCase_thenCorrect() {
|
||||
String s = "WELCOME to BAELDUNG!";
|
||||
|
||||
assertEquals("welcome to baeldung!", s.toLowerCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallReplace_thenCorrect() {
|
||||
String s = "I learn Spanish";
|
||||
|
||||
assertEquals("I learn French", s.replaceAll("Spanish", "French"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIntern_thenCorrect() {
|
||||
String s1 = "abc";
|
||||
String s2 = new String("abc");
|
||||
String s3 = new String("foo");
|
||||
String s4 = s1.intern();
|
||||
String s5 = s2.intern();
|
||||
|
||||
assertFalse(s3 == s4);
|
||||
assertTrue(s1 == s5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallValueOf_thenCorrect() {
|
||||
long l = 200L;
|
||||
|
||||
assertEquals("200", String.valueOf(l));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class TitleCaseConverterUnitTest {
|
||||
|
||||
private static final String TEXT = "tHis IS a tiTLe";
|
||||
private static final String TEXT_EXPECTED = "This Is A Title";
|
||||
private static final String TEXT_EXPECTED_NOT_FULL = "THis IS A TiTLe";
|
||||
|
||||
private static final String TEXT_OTHER_DELIMITERS = "tHis, IS a tiTLe";
|
||||
private static final String TEXT_EXPECTED_OTHER_DELIMITERS = "This, Is A Title";
|
||||
private static final String TEXT_EXPECTED_OTHER_DELIMITERS_NOT_FULL = "THis, IS A TiTLe";
|
||||
|
||||
@Test
|
||||
public void whenConvertingToTitleCaseIterating_thenStringConverted() {
|
||||
assertEquals(TEXT_EXPECTED, TitleCaseConverter.convertToTitleCaseIteratingChars(TEXT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertingToTitleCaseSplitting_thenStringConverted() {
|
||||
assertEquals(TEXT_EXPECTED, TitleCaseConverter.convertToTitleCaseSplitting(TEXT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertingToTitleCaseUsingWordUtilsFull_thenStringConverted() {
|
||||
assertEquals(TEXT_EXPECTED, TitleCaseConverter.convertToTileCaseWordUtilsFull(TEXT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertingToTitleCaseUsingWordUtils_thenStringConvertedOnlyFirstCharacter() {
|
||||
assertEquals(TEXT_EXPECTED_NOT_FULL, TitleCaseConverter.convertToTileCaseWordUtils(TEXT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertingToTitleCaseUsingIcu4j_thenStringConverted() {
|
||||
assertEquals(TEXT_EXPECTED, TitleCaseConverter.convertToTitleCaseIcu4j(TEXT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertingToTitleCaseWithDifferentDelimiters_thenDelimitersKept() {
|
||||
assertEquals(TEXT_EXPECTED_OTHER_DELIMITERS, TitleCaseConverter.convertToTitleCaseIteratingChars(TEXT_OTHER_DELIMITERS));
|
||||
assertEquals(TEXT_EXPECTED_OTHER_DELIMITERS, TitleCaseConverter.convertToTitleCaseSplitting(TEXT_OTHER_DELIMITERS));
|
||||
assertEquals(TEXT_EXPECTED_OTHER_DELIMITERS, TitleCaseConverter.convertToTileCaseWordUtilsFull(TEXT_OTHER_DELIMITERS));
|
||||
assertEquals(TEXT_EXPECTED_OTHER_DELIMITERS_NOT_FULL, TitleCaseConverter.convertToTileCaseWordUtils(TEXT_OTHER_DELIMITERS));
|
||||
assertEquals(TEXT_EXPECTED_OTHER_DELIMITERS, TitleCaseConverter.convertToTitleCaseIcu4j(TEXT_OTHER_DELIMITERS));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNull_whenConvertingToTileCase_thenReturnNull() {
|
||||
assertEquals(null, TitleCaseConverter.convertToTitleCaseIteratingChars(null));
|
||||
assertEquals(null, TitleCaseConverter.convertToTitleCaseSplitting(null));
|
||||
assertEquals(null, TitleCaseConverter.convertToTileCaseWordUtilsFull(null));
|
||||
assertEquals(null, TitleCaseConverter.convertToTileCaseWordUtils(null));
|
||||
assertEquals(null, TitleCaseConverter.convertToTitleCaseIcu4j(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyString_whenConvertingToTileCase_thenReturnEmptyString() {
|
||||
assertEquals("", TitleCaseConverter.convertToTitleCaseIteratingChars(""));
|
||||
assertEquals("", TitleCaseConverter.convertToTitleCaseSplitting(""));
|
||||
assertEquals("", TitleCaseConverter.convertToTileCaseWordUtilsFull(""));
|
||||
assertEquals("", TitleCaseConverter.convertToTileCaseWordUtils(""));
|
||||
assertEquals("", TitleCaseConverter.convertToTitleCaseIcu4j(""));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.baeldung.string.formatter;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Formatter;
|
||||
import java.util.GregorianCalendar;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
public class StringFormatterExampleUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenString_whenFormatSpecifierForCalendar_thenGotExpected() {
|
||||
//Syntax of Format Specifiers for Date/Time Representation
|
||||
Calendar c = new GregorianCalendar(2017, 11, 10);
|
||||
String s = String.format("The date is: %tm %1$te,%1$tY", c);
|
||||
|
||||
|
||||
assertEquals("The date is: 12 10,2017", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenGeneralConversion_thenConvertedString() {
|
||||
//General Conversions
|
||||
String s = String.format("The correct answer is %s", false);
|
||||
assertEquals("The correct answer is false", s);
|
||||
|
||||
s = String.format("The correct answer is %b", null);
|
||||
assertEquals("The correct answer is false", s);
|
||||
|
||||
s = String.format("The correct answer is %B", true);
|
||||
assertEquals("The correct answer is TRUE", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenCharConversion_thenConvertedString() {
|
||||
//Character Conversions
|
||||
String s = String.format("The correct answer is %c", 'a');
|
||||
assertEquals("The correct answer is a", s);
|
||||
|
||||
s = String.format("The correct answer is %c", null);
|
||||
assertEquals("The correct answer is null", s);
|
||||
|
||||
s = String.format("The correct answer is %C", 'b');
|
||||
assertEquals("The correct answer is B", s);
|
||||
|
||||
s = String.format("The valid unicode character: %c", 0x0400);
|
||||
assertTrue(Character.isValidCodePoint(0x0400));
|
||||
assertEquals("The valid unicode character: Ѐ", s);
|
||||
}
|
||||
|
||||
@Test(expected = java.util.IllegalFormatCodePointException.class)
|
||||
public void givenString_whenIllegalCodePointForConversion_thenError() {
|
||||
String s = String.format("The valid unicode character: %c", 0x11FFFF);
|
||||
assertFalse(Character.isValidCodePoint(0x11FFFF));
|
||||
assertEquals("The valid unicode character: Ā", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenNumericIntegralConversion_thenConvertedString() {
|
||||
//Numeric Integral Conversions
|
||||
String s = String.format("The number 25 in decimal = %d", 25);
|
||||
assertEquals("The number 25 in decimal = 25", s);
|
||||
|
||||
s = String.format("The number 25 in octal = %o", 25);
|
||||
assertEquals("The number 25 in octal = 31", s);
|
||||
|
||||
s = String.format("The number 25 in hexadecimal = %x", 25);
|
||||
assertEquals("The number 25 in hexadecimal = 19", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenNumericFloatingConversion_thenConvertedString() {
|
||||
//Numeric Floating-point Conversions
|
||||
String s = String.format("The computerized scientific format of 10000.00 "
|
||||
+ "= %e", 10000.00);
|
||||
assertEquals("The computerized scientific format of 10000.00 = 1.000000e+04", s);
|
||||
|
||||
s = String.format("The decimal format of 10.019 = %f", 10.019);
|
||||
assertEquals("The decimal format of 10.019 = 10.019000", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenLineSeparatorConversion_thenConvertedString() {
|
||||
//Line Separator Conversion
|
||||
String s = String.format("First Line %nSecond Line");
|
||||
assertEquals("First Line " + System.getProperty("line.separator")
|
||||
+ "Second Line", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenSpecifyFlag_thenGotFormattedString() {
|
||||
//Without left-justified flag
|
||||
String s = String.format("Without left justified flag: %5d", 25);
|
||||
assertEquals("Without left justified flag: 25", s);
|
||||
|
||||
//Using left-justified flag
|
||||
s = String.format("With left justified flag: %-5d", 25);
|
||||
assertEquals("With left justified flag: 25 ", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenSpecifyPrecision_thenGotExpected() {
|
||||
|
||||
//Precision
|
||||
String s = String.format("Output of 25.09878 with Precision 2: %.2f", 25.09878);
|
||||
assertEquals("Output of 25.09878 with Precision 2: 25.10", s);
|
||||
|
||||
s = String.format("Output of general conversion type with Precision 2: %.2b", true);
|
||||
assertEquals("Output of general conversion type with Precision 2: tr", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenSpecifyArgumentIndex_thenGotExpected() {
|
||||
Calendar c = new GregorianCalendar(2017, 11, 10);
|
||||
//Argument_Index
|
||||
String s = String.format("The date is: %tm %1$te,%1$tY", c);
|
||||
assertEquals("The date is: 12 10,2017", s);
|
||||
|
||||
s = String.format("The date is: %tm %<te,%<tY", c);
|
||||
assertEquals("The date is: 12 10,2017", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAppendable_whenCreateFormatter_thenFormatterWorksOnAppendable() {
|
||||
//Using String Formatter with Appendable
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Formatter formatter = new Formatter(sb);
|
||||
formatter.format("I am writting to a %s Instance.", sb.getClass());
|
||||
assertEquals("I am writting to a class java.lang.StringBuilder Instance.", sb.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenNoArguments_thenExpected() {
|
||||
//Using String Formatter without arguments
|
||||
String s = String.format("John scored 90%% in Fall semester");
|
||||
assertEquals("John scored 90% in Fall semester", s);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user