[JAVA-33592] Created new module core-java-string-operations-9 and moved code from core-java-string-operations-7 (#16364)

This commit is contained in:
panos-kakos 2024-04-22 00:29:58 +03:00 committed by GitHub
parent 2735ad0e84
commit 30d09bdc45
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 164 additions and 107 deletions

View File

@ -13,4 +13,4 @@ This module contains articles about string operations.
- [Encode a String to UTF-8 in Java](https://www.baeldung.com/java-string-encode-utf-8)
- [Guide to Character Encoding](https://www.baeldung.com/java-char-encoding)
- [Convert Hex to ASCII in Java](https://www.baeldung.com/java-convert-hex-to-ascii)
- More articles: [[<-- prev]](../core-java-string-operations)
- More articles: [[<-- prev]](../core-java-string-operations) [[next -->]](../core-java-string-operations-3)

View File

@ -10,3 +10,5 @@
- [Remove Beginning and Ending Double Quotes from a String](https://www.baeldung.com/java-remove-start-end-double-quote)
- [Splitting a Java String by Multiple Delimiters](https://www.baeldung.com/java-string-split-multiple-delimiters)
- [Split a String Only on the First Occurrence of Delimiter](https://www.baeldung.com/java-split-string-first-delimiter)
- More articles: [[<-- prev]](../core-java-string-operations-2) [[next -->]](../core-java-string-operations-4)

View File

@ -10,3 +10,4 @@
- [How to Truncate a String in Java](https://www.baeldung.com/java-truncating-strings)
- [Remove Whitespace From a String in Java](https://www.baeldung.com/java-string-remove-whitespace)
- [Named Placeholders in String Formatting](https://www.baeldung.com/java-string-formatting-named-placeholders)
- More articles: [[<-- prev]](../core-java-string-operations-3) [[next -->]](../core-java-string-operations-5)

View File

@ -11,3 +11,4 @@
- [Check if the First Letter of a String Is a Number](https://www.baeldung.com/java-check-if-string-starts-with-number)
- [Print “” Quotes Around a String in Java](https://www.baeldung.com/java-string-print-quotes)
- [Remove Punctuation From a String in Java](https://www.baeldung.com/java-remove-punctuation-from-string)
- More articles: [[<-- prev]](../core-java-string-operations-4) [[next -->]](../core-java-string-operations-6)

View File

@ -11,3 +11,4 @@
- [Check if a String Has All Unique Characters in Java](https://www.baeldung.com/java-check-string-all-unique-chars)
- [Performance Comparison Between Different Java String Concatenation Methods](https://www.baeldung.com/java-string-concatenation-methods)
- [Replacing Single Quote with \ in Java String](https://www.baeldung.com/java-replacing-single-quote-string)
- More articles: [[<-- prev]](../core-java-string-operations-5) [[next -->]](../core-java-string-operations-7)

View File

@ -2,17 +2,12 @@
### Relevant Articles:
- [How to Center Text Output in Java](https://www.baeldung.com/java-center-text-output)
- [Capitalize the First Letter of Each Word in a String](https://www.baeldung.com/java-string-initial-capital-letter-every-word)
- [Check if a String Contains Only Unicode Letters](https://www.baeldung.com/java-string-all-unicode-characters)
- [Create a “Mutable” String in Java](https://www.baeldung.com/java-mutable-string)
- [Check if a String Contains a Number Value in Java](https://www.baeldung.com/java-string-number-presence)
- [Difference Between String isEmpty() and isBlank()](https://www.baeldung.com/java-string-isempty-vs-isblank)
- [Strings Maximum Length in Java](https://www.baeldung.com/java-strings-maximum-length)
- [Javas String.length() and String.getBytes().length](https://www.baeldung.com/java-string-length-vs-getbytes-length)
- [Replace Non-Printable Unicode Characters in Java](https://www.baeldung.com/java-replace-non-printable-unicode-characters)
- [Check If a Java StringBuilder Object Contains a Character](https://www.baeldung.com/java-check-stringbuilder-object-contains-character)
- [Comparing One String With Multiple Values in One Expression in Java](https://www.baeldung.com/java-compare-string-multiple-values-one-expression)
- [UTF-8 Validation in Java](https://www.baeldung.com/java-utf-8-validation)
- [Simple Morse Code Translation in Java](https://www.baeldung.com/java-morse-code-english-translate)
- [How to Determine if a String Contains Invalid Encoded Characters](https://www.baeldung.com/java-check-string-contains-invalid-encoded-characters)
- [Regular Expression for Password Validation in Java](https://www.baeldung.com/java-regex-password-validation)
- [Mask an Email Address and Phone Number in Java](https://www.baeldung.com/java-mask-email-address-phone-number)
- More articles: [[<-- prev]](../core-java-string-operations-6) [[next -->]](../core-java-string-operations-8)

View File

@ -24,36 +24,10 @@
<artifactId>commons-text</artifactId>
<version>${commons-text.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>${apache.tika.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers-standard-package</artifactId>
<version>${apache.tika.version}</version>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>${icu4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${apache.commons.collection.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
@ -71,8 +45,7 @@
</build>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<apache.commons.lang3.version>3.13.0</apache.commons.lang3.version>
<apache.commons.collection.version>4.4</apache.commons.collection.version>
<apache.tika.version>2.9.1</apache.tika.version>

View File

@ -10,3 +10,7 @@
- [Converting UTF-8 to ISO-8859-1 in Java](https://www.baeldung.com/java-utf-8-iso-8859-1-conversion)
- [Get Last n Characters From a String](https://www.baeldung.com/java-string-get-last-n-characters)
- [Find the Length of the Longest Symmetric Substring](https://www.baeldung.com/java-find-length-longest-symmetric-substring)
- More articles: [[<-- prev]](../core-java-string-operations-7) [[next -->]](../core-java-string-operations-9)

View File

@ -0,0 +1,9 @@
### Relevant Articles:
- [Check if a String Contains Only Unicode Letters](https://www.baeldung.com/java-string-all-unicode-characters)
- [Difference Between String isEmpty() and isBlank()](https://www.baeldung.com/java-string-isempty-vs-isblank)
- [Replace Non-Printable Unicode Characters in Java](https://www.baeldung.com/java-replace-non-printable-unicode-characters)
- [UTF-8 Validation in Java](https://www.baeldung.com/java-utf-8-validation)
- [Simple Morse Code Translation in Java](https://www.baeldung.com/java-morse-code-english-translate)
- [How to Determine if a String Contains Invalid Encoded Characters](https://www.baeldung.com/java-check-string-contains-invalid-encoded-characters)
- More articles: [[<-- prev]](../core-java-string-operations-8)

View File

@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<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-operations-9</artifactId>
<packaging>jar</packaging>
<name>core-java-string-operations-9</name>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache.commons.lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${commons-text.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>${apache.tika.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers-standard-package</artifactId>
<version>${apache.tika.version}</version>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>${icu4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${apache.commons.collection.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<apache.commons.lang3.version>3.13.0</apache.commons.lang3.version>
<apache.commons.collection.version>4.4</apache.commons.collection.version>
<apache.tika.version>2.9.1</apache.tika.version>
<commons-text.version>1.10.0</commons-text.version>
<icu4j.version>74.1</icu4j.version>
</properties>
</project>

View File

@ -1,33 +1,33 @@
package com.baeldung.checkifstringcontainsinvalidcharacters;
import org.junit.jupiter.api.Test;
import java.nio.charset.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class CheckIfStringContainsInvalidEncodedCharactersUnitTest {
public String input = "HÆllo, World!";
@Test
public void givenInputString_whenUsingRegexPattern_thenFindIfInvalidCharacters() {
String regexPattern = "[^\\x00-\\x7F]+";
Pattern pattern = Pattern.compile(regexPattern);
Matcher matcher = pattern.matcher(input);
assertTrue(matcher.find());
}
@Test
public void givenInputString_whenUsingStringEncoding_thenFindIfInvalidCharacters() {
byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
boolean found = false;
for (byte b : bytes) {
found = (b & 0xFF) > 127 ? true : found;
}
assertTrue(found);
}
}
package com.baeldung.checkifstringcontainsinvalidcharacters;
import org.junit.jupiter.api.Test;
import java.nio.charset.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class CheckIfStringContainsInvalidEncodedCharactersUnitTest {
public String input = "HÆllo, World!";
@Test
public void givenInputString_whenUsingRegexPattern_thenFindIfInvalidCharacters() {
String regexPattern = "[^\\x00-\\x7F]+";
Pattern pattern = Pattern.compile(regexPattern);
Matcher matcher = pattern.matcher(input);
assertTrue(matcher.find());
}
@Test
public void givenInputString_whenUsingStringEncoding_thenFindIfInvalidCharacters() {
byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
boolean found = false;
for (byte b : bytes) {
found = (b & 0xFF) > 127 ? true : found;
}
assertTrue(found);
}
}

View File

@ -1,39 +1,39 @@
package com.baeldung.replacenonprintablecharacters;
import org.junit.jupiter.api.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ReplaceNonPrintableCharsUnitTest {
@Test
public void givenTextWithNonPrintableChars_whenUsingRegularExpression_thenGetSanitizedText() {
String originalText = "\n\nWelcome \n\n\n\tto Baeldung!\n\t";
String expected = "Welcome to Baeldung!";
String regex = "[\\p{C}]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(originalText);
String sanitizedText = matcher.replaceAll("");
assertEquals(expected, sanitizedText);
}
@Test
public void givenTextWithNonPrintableChars_whenCustomImplementation_thenGetSanitizedText() {
String originalText = "\n\nWelcome \n\n\n\tto Baeldung!\n\t";
String expected = "Welcome to Baeldung!";
StringBuilder strBuilder = new StringBuilder();
originalText.codePoints().forEach((i) -> {
if (i >= 32 && i != 127) {
strBuilder.append(Character.toChars(i));
}
});
assertEquals(expected, strBuilder.toString());
}
}
package com.baeldung.replacenonprintablecharacters;
import org.junit.jupiter.api.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ReplaceNonPrintableCharsUnitTest {
@Test
public void givenTextWithNonPrintableChars_whenUsingRegularExpression_thenGetSanitizedText() {
String originalText = "\n\nWelcome \n\n\n\tto Baeldung!\n\t";
String expected = "Welcome to Baeldung!";
String regex = "[\\p{C}]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(originalText);
String sanitizedText = matcher.replaceAll("");
assertEquals(expected, sanitizedText);
}
@Test
public void givenTextWithNonPrintableChars_whenCustomImplementation_thenGetSanitizedText() {
String originalText = "\n\nWelcome \n\n\n\tto Baeldung!\n\t";
String expected = "Welcome to Baeldung!";
StringBuilder strBuilder = new StringBuilder();
originalText.codePoints().forEach((i) -> {
if (i >= 32 && i != 127) {
strBuilder.append(Character.toChars(i));
}
});
assertEquals(expected, strBuilder.toString());
}
}

View File

@ -205,6 +205,7 @@
<module>core-java-string-operations-6</module>
<module>core-java-string-operations-7</module>
<module>core-java-string-operations-8</module>
<module>core-java-string-operations-9</module>
<module>core-java-regex</module>
<module>core-java-regex-2</module>
<module>core-java-regex-3</module>