Move articles out of java-strings part1
This commit is contained in:
@@ -4,21 +4,16 @@ This module contains articles about strings in Java.
|
||||
|
||||
### Relevant Articles:
|
||||
- [String Operations with Java Streams](https://www.baeldung.com/java-stream-operations-on-strings)
|
||||
- [Converting String to Stream of chars](https://www.baeldung.com/java-string-to-stream)
|
||||
- [Java 8 StringJoiner](https://www.baeldung.com/java-string-joiner)
|
||||
- [Converting Strings to Enums in Java](https://www.baeldung.com/java-string-to-enum)
|
||||
- [Quick Guide to the Java StringTokenizer](https://www.baeldung.com/java-stringtokenizer)
|
||||
- [Use char[] Array Over a String for Manipulating Passwords in Java?](https://www.baeldung.com/java-storing-passwords)
|
||||
- [Convert a String to Title Case](https://www.baeldung.com/java-string-title-case)
|
||||
- [Compact Strings in Java 9](https://www.baeldung.com/java-9-compact-string)
|
||||
- [Java Check a String for Lowercase/Uppercase Letter, Special Character and Digit](https://www.baeldung.com/java-lowercase-uppercase-special-character-digit-regex)
|
||||
- [Convert java.util.Date to String](https://www.baeldung.com/java-util-date-to-string)
|
||||
- [Converting a Stack Trace to a String in Java](https://www.baeldung.com/java-stacktrace-to-string)
|
||||
- [Sorting a String Alphabetically in Java](https://www.baeldung.com/java-sort-string-alphabetically)
|
||||
- [String Not Empty Test Assertions in Java](https://www.baeldung.com/java-assert-string-not-empty)
|
||||
- [String Performance Hints](https://www.baeldung.com/java-string-performance)
|
||||
- [Using indexOf to Find All Occurrences of a Word in a String](https://www.baeldung.com/java-indexOf-find-string-occurrences)
|
||||
- [Adding a Newline Character to a String in Java](https://www.baeldung.com/java-string-newline)
|
||||
- [Remove or Replace part of a String in Java](https://www.baeldung.com/java-remove-replace-string-part)
|
||||
- [Remove or Replace Part of a String in Java](https://www.baeldung.com/java-remove-replace-string-part)
|
||||
- [Replace a Character at a Specific Index in a String in Java](https://www.baeldung.com/java-replace-character-at-index)
|
||||
- More articles: [[next -->]](/java-strings-2)
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.enums;
|
||||
|
||||
public enum PizzaStatusEnum {
|
||||
ORDERED(5) {
|
||||
@Override
|
||||
public boolean isOrdered() {
|
||||
return true;
|
||||
}
|
||||
},
|
||||
READY(2) {
|
||||
@Override
|
||||
public boolean isReady() {
|
||||
return true;
|
||||
}
|
||||
},
|
||||
DELIVERED(0) {
|
||||
@Override
|
||||
public boolean isDelivered() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
private int timeToDelivery;
|
||||
|
||||
public boolean isOrdered() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isReady() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isDelivered() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getTimeToDelivery() {
|
||||
return timeToDelivery;
|
||||
}
|
||||
|
||||
PizzaStatusEnum(int timeToDelivery) {
|
||||
this.timeToDelivery = timeToDelivery;
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.lang3.text.WordUtils;
|
||||
|
||||
import com.ibm.icu.lang.UCharacter;
|
||||
import com.ibm.icu.text.BreakIterator;
|
||||
|
||||
public class TitleCaseConverter {
|
||||
|
||||
private static final String WORD_SEPARATOR = " ";
|
||||
|
||||
public static String convertToTitleCaseIteratingChars(String text) {
|
||||
if (text == null || text.isEmpty()) {
|
||||
return text;
|
||||
}
|
||||
|
||||
StringBuilder converted = new StringBuilder();
|
||||
|
||||
boolean convertNext = true;
|
||||
for (char ch : text.toCharArray()) {
|
||||
if (Character.isSpaceChar(ch)) {
|
||||
convertNext = true;
|
||||
} else if (convertNext) {
|
||||
ch = Character.toTitleCase(ch);
|
||||
convertNext = false;
|
||||
} else {
|
||||
ch = Character.toLowerCase(ch);
|
||||
}
|
||||
converted.append(ch);
|
||||
}
|
||||
|
||||
return converted.toString();
|
||||
}
|
||||
|
||||
public static String convertToTitleCaseSplitting(String text) {
|
||||
if (text == null || text.isEmpty()) {
|
||||
return text;
|
||||
}
|
||||
|
||||
return Arrays
|
||||
.stream(text.split(WORD_SEPARATOR))
|
||||
.map(word -> word.isEmpty()
|
||||
? word
|
||||
: Character.toTitleCase(word.charAt(0)) + word
|
||||
.substring(1)
|
||||
.toLowerCase())
|
||||
.collect(Collectors.joining(WORD_SEPARATOR));
|
||||
}
|
||||
|
||||
public static String convertToTitleCaseIcu4j(String text) {
|
||||
if (text == null || text.isEmpty()) {
|
||||
return text;
|
||||
}
|
||||
|
||||
return UCharacter.toTitleCase(text, BreakIterator.getTitleInstance());
|
||||
}
|
||||
|
||||
public static String convertToTileCaseWordUtilsFull(String text) {
|
||||
return WordUtils.capitalizeFully(text);
|
||||
}
|
||||
|
||||
public static String convertToTileCaseWordUtils(String text) {
|
||||
return WordUtils.capitalize(text);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package com.baeldung;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.primitives.Ints;
|
||||
|
||||
public class StringToIntOrIntegerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenString_whenParsingInt_shouldConvertToInt() {
|
||||
String givenString = "42";
|
||||
|
||||
int result = Integer.parseInt(givenString);
|
||||
|
||||
assertThat(result).isEqualTo(42);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenCallingIntegerValueOf_shouldConvertToInt() {
|
||||
String givenString = "42";
|
||||
|
||||
Integer result = Integer.valueOf(givenString);
|
||||
|
||||
assertThat(result).isEqualTo(new Integer(42));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenCallingIntegerConstructor_shouldConvertToInt() {
|
||||
String givenString = "42";
|
||||
|
||||
Integer result = new Integer(givenString);
|
||||
|
||||
assertThat(result).isEqualTo(new Integer(42));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenCallingIntegerDecode_shouldConvertToInt() {
|
||||
String givenString = "42";
|
||||
|
||||
int result = Integer.decode(givenString);
|
||||
|
||||
assertThat(result).isEqualTo(42);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenTryParse_shouldConvertToInt() {
|
||||
String givenString = "42";
|
||||
|
||||
Integer result = Ints.tryParse(givenString);
|
||||
|
||||
assertThat(result).isEqualTo(42);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void givenInvalidInput_whenParsingInt_shouldThrow() {
|
||||
String givenString = "nan";
|
||||
Integer.parseInt(givenString);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.baeldung.enums;
|
||||
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class PizzaUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenConvertedIntoEnum_thenGetsConvertedCorrectly() {
|
||||
String pizzaEnumValue = "READY";
|
||||
PizzaStatusEnum pizzaStatusEnum = PizzaStatusEnum.valueOf(pizzaEnumValue);
|
||||
assertTrue(pizzaStatusEnum == PizzaStatusEnum.READY);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void whenConvertedIntoEnum_thenThrowsException() {
|
||||
String pizzaEnumValue = "rEAdY";
|
||||
PizzaStatusEnum pizzaStatusEnum = PizzaStatusEnum.valueOf(pizzaEnumValue);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenInvalidEnumValueContentWiseAsString_whenConvertedIntoEnum_thenThrowsException() {
|
||||
String pizzaEnumValue = "invalid";
|
||||
PizzaStatusEnum pizzaStatusEnum = PizzaStatusEnum.valueOf(pizzaEnumValue);
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
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(""));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user