Merge branch 'master' into BAEL-18260
This commit is contained in:
@@ -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)
|
||||
47
core-java-modules/core-java-string-conversions-2/pom.xml
Normal file
47
core-java-modules/core-java-string-conversions-2/pom.xml
Normal 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>
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.datetime;
|
||||
package com.baeldung.stringconversions;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@@ -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;
|
||||
@@ -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
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
16
core-java-modules/core-java-string-conversions/README.md
Normal file
16
core-java-modules/core-java-string-conversions/README.md
Normal file
@@ -0,0 +1,16 @@
|
||||
## 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)
|
||||
67
core-java-modules/core-java-string-conversions/pom.xml
Normal file
67
core-java-modules/core-java-string-conversions/pom.xml
Normal 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>
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.enums;
|
||||
package com.baeldung.stringtoenum;
|
||||
|
||||
public enum PizzaStatusEnum {
|
||||
ORDERED(5) {
|
||||
@@ -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 {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung;
|
||||
package com.baeldung.chartostring;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.string.todouble;
|
||||
package com.baeldung.stringtodouble;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@@ -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() {
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung;
|
||||
package com.baeldung.stringtoint;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.string;
|
||||
package com.baeldung.stringtostream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.string;
|
||||
package com.baeldung.titlecase;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
Before Width: | Height: | Size: 2.8 KiB After Width: | Height: | Size: 2.8 KiB |
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
Relevant Articles:
|
||||
- [Java String Conversions](http://www.baeldung.com/java-string-conversions)
|
||||
@@ -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)
|
||||
|
||||
5
pom.xml
5
pom.xml
@@ -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>
|
||||
@@ -605,6 +607,7 @@
|
||||
|
||||
<module>spring-boot-nashorn</module>
|
||||
<module>java-blockchain</module>
|
||||
<module>wildfly</module>
|
||||
|
||||
</modules>
|
||||
|
||||
@@ -1187,6 +1190,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>
|
||||
|
||||
@@ -6,4 +6,5 @@ This module contains articles about reactive Spring 5 WebClient
|
||||
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
|
||||
### Relevant Articles
|
||||
- [Simultaneous Spring WebClient Calls](https://www.baeldung.com/spring-webclient-simultaneous-calls)
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.thymeleaf.pathvariables;
|
||||
|
||||
public class Detail {
|
||||
private int id;
|
||||
private String description;
|
||||
|
||||
public Detail(int id, String description) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.thymeleaf.pathvariables;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Item {
|
||||
private int id;
|
||||
private String name;
|
||||
private List<Detail> details;
|
||||
|
||||
public Item(int id, String name) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<Detail> getDetails() {
|
||||
return details;
|
||||
}
|
||||
|
||||
public void setDetails(List<Detail> details) {
|
||||
this.details = details;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.thymeleaf.pathvariables;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
@Controller
|
||||
public class PathVariablesController {
|
||||
private List<Item> items = new ArrayList<Item>();
|
||||
|
||||
public PathVariablesController() {
|
||||
Item item1 = new Item(1, "First Item");
|
||||
List<Detail> item1Details = new ArrayList<>();
|
||||
item1Details.add(new Detail(1, "Green"));
|
||||
item1Details.add(new Detail(2, "Large"));
|
||||
item1.setDetails(item1Details);
|
||||
items.add(item1);
|
||||
|
||||
Item item2 = new Item(2, "Second Item");
|
||||
List<Detail> item2Details = new ArrayList<>();
|
||||
item2Details.add(new Detail(1, "Red"));
|
||||
item2Details.add(new Detail(2, "Medium"));
|
||||
item2.setDetails(item2Details);
|
||||
items.add(item2);
|
||||
}
|
||||
|
||||
@GetMapping("/pathvars")
|
||||
public String start(Model model) {
|
||||
model.addAttribute("items", items);
|
||||
return "pathvariables/index";
|
||||
}
|
||||
|
||||
@GetMapping("/pathvars/single/{id}")
|
||||
public String singlePathVariable(@PathVariable("id") int id, Model model) {
|
||||
if (id == 1) {
|
||||
model.addAttribute("item", new Item(1, "First Item"));
|
||||
} else {
|
||||
model.addAttribute("item", new Item(2, "Second Item"));
|
||||
}
|
||||
|
||||
return "pathvariables/view";
|
||||
}
|
||||
|
||||
@GetMapping("/pathvars/item/{itemId}/detail/{detailId}")
|
||||
public String multiplePathVariable(@PathVariable("itemId") int itemId, @PathVariable("detailId") int detailId, Model model) {
|
||||
for (Item item : items) {
|
||||
if (item.getId() == itemId) {
|
||||
model.addAttribute("item", item);
|
||||
for (Detail detail : item.getDetails()) {
|
||||
if (detail.getId() == detailId) {
|
||||
model.addAttribute("detail", detail);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return "pathvariables/view";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>PathVariables in Thymeleaf</title>
|
||||
</head>
|
||||
<body>
|
||||
<h3>Items</h3>
|
||||
<div th:each="item : ${items}">
|
||||
<a th:href="@{/pathvars/single/{id}(id = ${item.id})}">
|
||||
<span th:text="${item.name}"></span>
|
||||
</a>
|
||||
<ul>
|
||||
<li th:each="detail : ${item.details}">
|
||||
<a th:href="@{/pathvars/item/{itemId}/detail/{detailId}(itemId = ${item.id}, detailId = ${detail.id})}">
|
||||
<span th:text="${detail.description}"></span>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,19 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Pathvariables in Thymeleaf</title>
|
||||
</head>
|
||||
<body>
|
||||
<h3>View Item</h3>
|
||||
<div>
|
||||
<label for="name">Name: </label>
|
||||
<span th:text="${item.name}"></span>
|
||||
</div>
|
||||
<h4 th:if="${detail}">Detail</h4>
|
||||
<div th:if="${detail}">
|
||||
<label for="description">Description: </label>
|
||||
<span th:text="${detail.description}"></span>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -4,8 +4,9 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>gs-spring-boot-wildfly</artifactId>
|
||||
<artifactId>wildfly</artifactId>
|
||||
<version>0.1.0</version>
|
||||
<name>wildfly</name>
|
||||
|
||||
<packaging>war</packaging>
|
||||
|
||||
@@ -38,11 +39,11 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- end::tests[] -->
|
||||
</dependencies>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user