Merge pull request #7990 from catalin-burcea/BAEL-18261

[BAEL-18261] Move articles out of java-strings part1
This commit is contained in:
Josh Cummings
2019-10-14 08:00:03 -06:00
committed by GitHub
27 changed files with 309 additions and 181 deletions

View File

@@ -0,0 +1,9 @@
## Java String Conversions
This module contains articles about string conversions from/to another type.
### Relevant Articles:
- [Java String Conversions](https://www.baeldung.com/java-string-conversions)
- [Convert String to Byte Array and Reverse in Java](https://www.baeldung.com/java-string-to-byte-array)
- [Convert Char Array to String](https://www.baeldung.com/java-char-array-to-string)
- More articles: [[<-- prev]](/core-java-string-conversions)

View File

@@ -0,0 +1,47 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-string-conversions-2</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>core-java-string-conversions-2</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-string-conversions-2</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>

View File

@@ -1,4 +1,4 @@
package com.baeldung.datetime;
package com.baeldung.stringconversions;
import java.time.LocalDateTime;

View File

@@ -1,4 +1,4 @@
package com.baeldung.string.charArrayToString;
package com.baeldung.chararraytostring;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

View File

@@ -1,4 +1,4 @@
package com.baeldung.java.conversion;
package com.baeldung.stringconversions;
import static org.junit.Assert.assertEquals;
@@ -13,8 +13,6 @@ import java.util.GregorianCalendar;
import org.junit.Test;
import com.baeldung.datetime.UseLocalDateTime;
public class StringConversionUnitTest {
@Test

View File

@@ -1,4 +1,4 @@
package com.baeldung.string.conversion;
package com.baeldung.stringtobytearray;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

View File

@@ -1,4 +1,4 @@
package com.baeldung.string.conversion;
package com.baeldung.stringtobytearray;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

View File

@@ -0,0 +1,17 @@
## Java String Conversions
This module contains articles about string conversions from/to another type.
### Relevant Articles:
- [Converting String to Stream of chars](https://www.baeldung.com/java-string-to-stream)
- [Converting Strings to Enums in Java](https://www.baeldung.com/java-string-to-enum)
- [Convert a String to Title Case](https://www.baeldung.com/java-string-title-case)
- [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)
- [Image to Base64 String Conversion](https://www.baeldung.com/java-base64-image-string)
- [Convert a Comma Separated String to a List in Java](https://www.baeldung.com/java-string-with-separator-to-list)
- [Converting Java String to Double](https://www.baeldung.com/java-string-to-double)
- [Convert Char to String in Java](https://www.baeldung.com/java-convert-char-to-string)
- [Convert String to int or Integer in Java](https://www.baeldung.com/java-convert-string-to-int-or-integer)
- More articles: [[next -->]](/core-java-string-conversions-2)
"Counting Words in a String

View File

@@ -0,0 +1,67 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-string-conversions</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>core-java-string-conversions</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>${icu4j.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-string-conversions</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<icu4j.version>61.1</icu4j.version>
<assertj.version>3.6.1</assertj.version>
</properties>
</project>

View File

@@ -1,4 +1,4 @@
package com.baeldung.enums;
package com.baeldung.stringtoenum;
public enum PizzaStatusEnum {
ORDERED(5) {

View File

@@ -1,12 +1,11 @@
package com.baeldung.string;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.apache.commons.lang3.text.WordUtils;
package com.baeldung.titlecase;
import com.ibm.icu.lang.UCharacter;
import com.ibm.icu.text.BreakIterator;
import org.apache.commons.lang3.text.WordUtils;
import java.util.Arrays;
import java.util.stream.Collectors;
public class TitleCaseConverter {

View File

@@ -1,6 +1,7 @@
package com.baeldung.string.formatter;
package com.baeldung.datetostring;
import static org.junit.Assert.assertEquals;
import org.junit.BeforeClass;
import org.junit.Test;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
@@ -13,8 +14,7 @@ import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class DateToStringFormatterUnitTest {

View File

@@ -1,13 +1,13 @@
package com.baeldung.fileToBase64StringConversion;
package com.baeldung.filetobase64conversion;
import static org.junit.Assert.assertTrue;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class FileToBase64StringConversionUnitTest {

View File

@@ -1,4 +1,4 @@
package com.baeldung.string.todouble;
package com.baeldung.stringtodouble;
import static org.junit.Assert.assertEquals;

View File

@@ -1,10 +1,10 @@
package com.baeldung.enums;
package com.baeldung.stringtoenum;
import static junit.framework.TestCase.assertTrue;
import org.junit.Test;
public class PizzaUnitTest {
public class StringToEnumUnitTest {
@Test
public void whenConvertedIntoEnum_thenGetsConvertedCorrectly() {

View File

@@ -1,4 +1,4 @@
package com.baeldung;
package com.baeldung.stringtoint;
import static org.assertj.core.api.Assertions.assertThat;

View File

@@ -1,135 +1,134 @@
package com.baeldung;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
public class ConvertStringToListUnitTest {
private final String countries = "Russia,Germany,England,France,Italy";
private final String ranks = "1,2,3,4,5, 6,7";
private final String emptyStrings = ",,,,,";
private final List<String> expectedCountriesList = Arrays.asList("Russia", "Germany", "England", "France", "Italy");
private final List<Integer> expectedRanksList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
private final List<String> expectedEmptyStringsList = Arrays.asList("", "", "", "", "", "");
@Test
public void givenString_thenGetListOfStringByJava() {
List<String> convertedCountriesList = Arrays.asList(countries.split(",", -1));
assertEquals(expectedCountriesList, convertedCountriesList);
}
@Test
public void givenString_thenGetListOfStringByApache() {
List<String> convertedCountriesList = Arrays.asList(StringUtils.splitPreserveAllTokens(countries, ","));
assertEquals(expectedCountriesList, convertedCountriesList);
}
@Test
public void givenString_thenGetListOfStringByGuava() {
List<String> convertedCountriesList = Splitter.on(",")
.trimResults()
.splitToList(countries);
assertEquals(expectedCountriesList, convertedCountriesList);
}
@Test
public void givenString_thenGetListOfStringByJava8() {
List<String> convertedCountriesList = Stream.of(countries.split(",", -1))
.collect(Collectors.toList());
assertEquals(expectedCountriesList, convertedCountriesList);
}
@Test
public void givenString_thenGetListOfIntegerByJava() {
String[] convertedRankArray = ranks.split(",");
List<Integer> convertedRankList = new ArrayList<Integer>();
for (String number : convertedRankArray) {
convertedRankList.add(Integer.parseInt(number.trim()));
}
assertEquals(expectedRanksList, convertedRankList);
}
@Test
public void givenString_thenGetListOfIntegerByGuava() {
List<Integer> convertedRankList = Lists.transform(Splitter.on(",")
.trimResults()
.splitToList(ranks), new Function<String, Integer>() {
@Override
public Integer apply(String input) {
return Integer.parseInt(input.trim());
}
});
assertEquals(expectedRanksList, convertedRankList);
}
@Test
public void givenString_thenGetListOfIntegerByJava8() {
List<Integer> convertedRankList = Stream.of(ranks.split(","))
.map(String::trim)
.map(Integer::parseInt)
.collect(Collectors.toList());
assertEquals(expectedRanksList, convertedRankList);
}
@Test
public void givenString_thenGetListOfIntegerByApache() {
String[] convertedRankArray = StringUtils.split(ranks, ",");
List<Integer> convertedRankList = new ArrayList<Integer>();
for (String number : convertedRankArray) {
convertedRankList.add(Integer.parseInt(number.trim()));
}
assertEquals(expectedRanksList, convertedRankList);
}
@Test
public void givenEmptyStrings_thenGetListOfStringByJava() {
List<String> convertedEmptyStringsList = Arrays.asList(emptyStrings.split(",", -1));
assertEquals(expectedEmptyStringsList, convertedEmptyStringsList);
}
@Test
public void givenEmptyStrings_thenGetListOfStringByApache() {
List<String> convertedEmptyStringsList = Arrays.asList(StringUtils.splitPreserveAllTokens(emptyStrings, ","));
assertEquals(expectedEmptyStringsList, convertedEmptyStringsList);
}
@Test
public void givenEmptyStrings_thenGetListOfStringByGuava() {
List<String> convertedEmptyStringsList = Splitter.on(",")
.trimResults()
.splitToList(emptyStrings);
assertEquals(expectedEmptyStringsList, convertedEmptyStringsList);
}
@Test
public void givenEmptyStrings_thenGetListOfStringByJava8() {
List<String> convertedEmptyStringsList = Stream.of(emptyStrings.split(",", -1))
.collect(Collectors.toList());
assertEquals(expectedEmptyStringsList, convertedEmptyStringsList);
}
}
package com.baeldung.stringtolist;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.assertEquals;
public class ConvertStringToListUnitTest {
private final String countries = "Russia,Germany,England,France,Italy";
private final String ranks = "1,2,3,4,5, 6,7";
private final String emptyStrings = ",,,,,";
private final List<String> expectedCountriesList = Arrays.asList("Russia", "Germany", "England", "France", "Italy");
private final List<Integer> expectedRanksList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
private final List<String> expectedEmptyStringsList = Arrays.asList("", "", "", "", "", "");
@Test
public void givenString_thenGetListOfStringByJava() {
List<String> convertedCountriesList = Arrays.asList(countries.split(",", -1));
assertEquals(expectedCountriesList, convertedCountriesList);
}
@Test
public void givenString_thenGetListOfStringByApache() {
List<String> convertedCountriesList = Arrays.asList(StringUtils.splitPreserveAllTokens(countries, ","));
assertEquals(expectedCountriesList, convertedCountriesList);
}
@Test
public void givenString_thenGetListOfStringByGuava() {
List<String> convertedCountriesList = Splitter.on(",")
.trimResults()
.splitToList(countries);
assertEquals(expectedCountriesList, convertedCountriesList);
}
@Test
public void givenString_thenGetListOfStringByJava8() {
List<String> convertedCountriesList = Stream.of(countries.split(",", -1))
.collect(Collectors.toList());
assertEquals(expectedCountriesList, convertedCountriesList);
}
@Test
public void givenString_thenGetListOfIntegerByJava() {
String[] convertedRankArray = ranks.split(",");
List<Integer> convertedRankList = new ArrayList<Integer>();
for (String number : convertedRankArray) {
convertedRankList.add(Integer.parseInt(number.trim()));
}
assertEquals(expectedRanksList, convertedRankList);
}
@Test
public void givenString_thenGetListOfIntegerByGuava() {
List<Integer> convertedRankList = Lists.transform(Splitter.on(",")
.trimResults()
.splitToList(ranks), new Function<String, Integer>() {
@Override
public Integer apply(String input) {
return Integer.parseInt(input.trim());
}
});
assertEquals(expectedRanksList, convertedRankList);
}
@Test
public void givenString_thenGetListOfIntegerByJava8() {
List<Integer> convertedRankList = Stream.of(ranks.split(","))
.map(String::trim)
.map(Integer::parseInt)
.collect(Collectors.toList());
assertEquals(expectedRanksList, convertedRankList);
}
@Test
public void givenString_thenGetListOfIntegerByApache() {
String[] convertedRankArray = StringUtils.split(ranks, ",");
List<Integer> convertedRankList = new ArrayList<Integer>();
for (String number : convertedRankArray) {
convertedRankList.add(Integer.parseInt(number.trim()));
}
assertEquals(expectedRanksList, convertedRankList);
}
@Test
public void givenEmptyStrings_thenGetListOfStringByJava() {
List<String> convertedEmptyStringsList = Arrays.asList(emptyStrings.split(",", -1));
assertEquals(expectedEmptyStringsList, convertedEmptyStringsList);
}
@Test
public void givenEmptyStrings_thenGetListOfStringByApache() {
List<String> convertedEmptyStringsList = Arrays.asList(StringUtils.splitPreserveAllTokens(emptyStrings, ","));
assertEquals(expectedEmptyStringsList, convertedEmptyStringsList);
}
@Test
public void givenEmptyStrings_thenGetListOfStringByGuava() {
List<String> convertedEmptyStringsList = Splitter.on(",")
.trimResults()
.splitToList(emptyStrings);
assertEquals(expectedEmptyStringsList, convertedEmptyStringsList);
}
@Test
public void givenEmptyStrings_thenGetListOfStringByJava8() {
List<String> convertedEmptyStringsList = Stream.of(emptyStrings.split(",", -1))
.collect(Collectors.toList());
assertEquals(expectedEmptyStringsList, convertedEmptyStringsList);
}
}

View File

@@ -1,4 +1,4 @@
package com.baeldung.string;
package com.baeldung.titlecase;
import org.junit.Assert;
import org.junit.Test;

View File

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@@ -8,14 +8,12 @@ This module contains articles about strings in Java.
- [Check If a String Contains a Substring](https://www.baeldung.com/java-string-contains-substring)
- [Removing Stopwords from a String in Java](https://www.baeldung.com/java-string-remove-stopwords)
- [Java Generate Random String](https://www.baeldung.com/java-random-string)
- [Image to Base64 String Conversion](https://www.baeldung.com/java-base64-image-string)
- [Java Base64 Encoding and Decoding](https://www.baeldung.com/java-base64-encode-and-decode)
- [Generate a Secure Random Password in Java](https://www.baeldung.com/java-generate-secure-password)
- [Removing Repeated Characters from a String](https://www.baeldung.com/java-remove-repeated-char)
- [Join Array of Primitives with Separator in Java](https://www.baeldung.com/java-join-primitive-array)
- [Pad a String with Zeros or Spaces in Java](https://www.baeldung.com/java-pad-string)
- [Remove Emojis from a Java String](https://www.baeldung.com/java-string-remove-emojis)
- [Convert a Comma Separated String to a List in Java](https://www.baeldung.com/java-string-with-separator-to-list)
- [Guide to java.util.Formatter](https://www.baeldung.com/java-string-formatter)
- [Remove Leading and Trailing Characters from a String](https://www.baeldung.com/java-remove-trailing-characters)
- [Concatenating Strings In Java](https://www.baeldung.com/java-strings-concatenation)

View File

@@ -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)

View File

@@ -3,10 +3,7 @@
This module contains articles about operations on strings in Java.
### Relevant Articles:
- [Convert char to String in Java](https://www.baeldung.com/java-convert-char-to-string)
- [Convert String to int or Integer in Java](https://www.baeldung.com/java-convert-string-to-int-or-integer)
- [Java String Conversions](https://www.baeldung.com/java-string-conversions)
- [Check if a String is a Palindrome](https://www.baeldung.com/java-palindrome)
- [Check If a String Is a Palindrome](https://www.baeldung.com/java-palindrome)
- [Comparing Strings in Java](https://www.baeldung.com/java-compare-strings)
- [Check If a String Is Numeric in Java](https://www.baeldung.com/java-check-string-number)
- [Get Substring from String in Java](https://www.baeldung.com/java-substring)
@@ -16,7 +13,6 @@ This module contains articles about operations on strings in Java.
- [Guide to Java String Pool](https://www.baeldung.com/java-string-pool)
- [Split a String in Java](https://www.baeldung.com/java-split-string)
- [Common String Operations in Java](https://www.baeldung.com/java-string-operations)
- [Convert String to Byte Array and Reverse in Java](https://www.baeldung.com/java-string-to-byte-array)
- [Java toString() Method](https://www.baeldung.com/java-tostring)
- [CharSequence vs. String in Java](https://www.baeldung.com/java-char-sequence-string)
- [StringBuilder and StringBuffer in Java](https://www.baeldung.com/java-string-builder-string-buffer)

View File

@@ -1,2 +0,0 @@
Relevant Articles:
- [Java String Conversions](http://www.baeldung.com/java-string-conversions)

View File

@@ -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)

View File

@@ -428,6 +428,8 @@
<module>core-java-modules/core-java-perf</module>
<module>core-java-modules/core-java-reflection</module>
<module>core-java-modules/core-java-sun</module>
<module>core-java-modules/core-java-string-conversions</module>
<module>core-java-modules/core-java-string-conversions-2</module>
<module>core-java-modules/core-java</module>
<module>core-java-modules/core-java-jvm</module>
<module>core-scala</module>
@@ -1189,6 +1191,8 @@
<module>core-java-modules/core-java-networking</module>
<module>core-java-modules/core-java-perf</module>
<module>core-java-modules/core-java-sun</module>
<module>core-java-modules/core-java-string-conversions</module>
<module>core-java-modules/core-java-string-conversions-2</module>
<module>core-scala</module>
<module>couchbase</module>
<module>custom-pmd</module>