Move articles out of java-strings part3
This commit is contained in:
@@ -6,11 +6,7 @@ This module contains articles about strings in Java.
|
||||
- [String Operations with Java Streams](https://www.baeldung.com/java-stream-operations-on-strings)
|
||||
- [Use char[] Array Over a String for Manipulating Passwords in Java?](https://www.baeldung.com/java-storing-passwords)
|
||||
- [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)
|
||||
- [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)
|
||||
- [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,29 +0,0 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
public class ReplaceCharacterInString {
|
||||
|
||||
public String replaceCharSubstring(String str, char ch, int index) {
|
||||
String myString = str.substring(0, index) + ch + str.substring(index + 1);
|
||||
return myString;
|
||||
}
|
||||
|
||||
public String replaceCharUsingCharArray(String str, char ch, int index) {
|
||||
char[] chars = str.toCharArray();
|
||||
chars[index] = ch;
|
||||
return String.valueOf(chars);
|
||||
}
|
||||
|
||||
|
||||
public String replaceCharStringBuilder(String str, char ch, int index) {
|
||||
StringBuilder myString = new StringBuilder(str);
|
||||
myString.setCharAt(index, ch);
|
||||
return myString.toString();
|
||||
}
|
||||
|
||||
public String replaceCharStringBuffer(String str, char ch, int index) {
|
||||
StringBuffer myString = new StringBuffer(str);
|
||||
myString.setCharAt(index, ch);
|
||||
return myString.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package com.baeldung.string.searching;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class WordIndexer {
|
||||
|
||||
public List<Integer> findWord(String textString, String word) {
|
||||
int index = 0;
|
||||
List<Integer> indexes = new ArrayList<Integer>();
|
||||
String lowerCaseTextString = textString.toLowerCase();
|
||||
String lowerCaseWord = word.toLowerCase();
|
||||
|
||||
while(index != -1) {
|
||||
index = lowerCaseTextString.indexOf(lowerCaseWord, index);
|
||||
if (index == -1) {
|
||||
break;
|
||||
}
|
||||
|
||||
indexes.add(index);
|
||||
index++;
|
||||
}
|
||||
return indexes;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<Integer> findWordUpgrade(String textString, String word) {
|
||||
int index = 0;
|
||||
List<Integer> indexes = new ArrayList<Integer>();
|
||||
StringBuilder output = new StringBuilder();
|
||||
String lowerCaseTextString = textString.toLowerCase();
|
||||
String lowerCaseWord = word.toLowerCase();
|
||||
int wordLength = 0;
|
||||
|
||||
while(index != -1){
|
||||
index = lowerCaseTextString.indexOf(lowerCaseWord, index + wordLength); // Slight improvement
|
||||
if (index != -1) {
|
||||
indexes.add(index);
|
||||
}
|
||||
wordLength = word.length();
|
||||
}
|
||||
return indexes;
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ReplaceCharInStringUnitTest {
|
||||
private ReplaceCharacterInString characterInString = new ReplaceCharacterInString();
|
||||
|
||||
@Test
|
||||
public void whenReplaceCharAtIndexUsingSubstring_thenSuccess() {
|
||||
assertEquals("abcme", characterInString.replaceCharSubstring("abcde", 'm', 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReplaceCharAtIndexUsingCharArray_thenSuccess() {
|
||||
assertEquals("abcme", characterInString.replaceCharUsingCharArray("abcde", 'm', 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReplaceCharAtIndexUsingStringBuilder_thenSuccess() {
|
||||
assertEquals("abcme", characterInString.replaceCharStringBuilder("abcde", 'm', 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReplaceCharAtIndexUsingStringBuffer_thenSuccess() {
|
||||
assertEquals("abcme", characterInString.replaceCharStringBuffer("abcde", 'm', 3));
|
||||
}
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
public class StringContainingCharactersUnitTest {
|
||||
|
||||
private static final Pattern[] inputRegexes = new Pattern[4];
|
||||
|
||||
private static final String regex = "^(?=.*?\\p{Lu})(?=.*?\\p{Ll})(?=.*?\\d)(?=.*?[`~!@#$%^&*()\\-_=+\\\\|\\[{\\]};:'\",<.>/?]).*$";
|
||||
|
||||
static {
|
||||
inputRegexes[0] = Pattern.compile(".*[A-Z].*");
|
||||
inputRegexes[1] = Pattern.compile(".*[a-z].*");
|
||||
inputRegexes[2] = Pattern.compile(".*\\d.*");
|
||||
inputRegexes[3] = Pattern.compile(".*[`~!@#$%^&*()\\-_=+\\\\|\\[{\\]};:'\",<.>/?].*");
|
||||
}
|
||||
|
||||
private static boolean isMatchingRegex(String input) {
|
||||
boolean inputMatches = true;
|
||||
for (Pattern inputRegex : inputRegexes) {
|
||||
if (!inputRegex
|
||||
.matcher(input)
|
||||
.matches()) {
|
||||
inputMatches = false;
|
||||
}
|
||||
}
|
||||
return inputMatches;
|
||||
}
|
||||
|
||||
private static boolean checkString(String input) {
|
||||
String specialChars = "~`!@#$%^&*()-_=+\\|[{]};:'\",<.>/?";
|
||||
char currentCharacter;
|
||||
boolean numberPresent = false;
|
||||
boolean upperCasePresent = false;
|
||||
boolean lowerCasePresent = false;
|
||||
boolean specialCharacterPresent = false;
|
||||
|
||||
for (int i = 0; i < input.length(); i++) {
|
||||
currentCharacter = input.charAt(i);
|
||||
if (Character.isDigit(currentCharacter)) {
|
||||
numberPresent = true;
|
||||
} else if (Character.isUpperCase(currentCharacter)) {
|
||||
upperCasePresent = true;
|
||||
} else if (Character.isLowerCase(currentCharacter)) {
|
||||
lowerCasePresent = true;
|
||||
} else if (specialChars.contains(String.valueOf(currentCharacter))) {
|
||||
specialCharacterPresent = true;
|
||||
}
|
||||
}
|
||||
|
||||
return numberPresent && upperCasePresent && lowerCasePresent && specialCharacterPresent;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegexes_whenMatchingCorrectString_thenMatches() {
|
||||
String validInput = "Ab3;";
|
||||
assertTrue(isMatchingRegex(validInput));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegexes_whenMatchingWrongStrings_thenNotMatching() {
|
||||
String invalidInput = "Ab3";
|
||||
assertFalse(isMatchingRegex(invalidInput));
|
||||
|
||||
invalidInput = "Ab;";
|
||||
assertFalse(isMatchingRegex(invalidInput));
|
||||
|
||||
invalidInput = "A3;";
|
||||
assertFalse(isMatchingRegex(invalidInput));
|
||||
|
||||
invalidInput = "b3;";
|
||||
assertFalse(isMatchingRegex(invalidInput));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidString_whenChecking_thenCorrect() {
|
||||
String validInput = "Ab3;";
|
||||
assertTrue(checkString(validInput));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidStrings_whenChecking_thenNotCorrect() {
|
||||
String invalidInput = "Ab3";
|
||||
assertFalse(checkString(invalidInput));
|
||||
|
||||
invalidInput = "Ab;";
|
||||
assertFalse(checkString(invalidInput));
|
||||
|
||||
invalidInput = "A3;";
|
||||
assertFalse(checkString(invalidInput));
|
||||
|
||||
invalidInput = "b3;";
|
||||
assertFalse(checkString(invalidInput));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSingleRegex_whenMatchingCorrectString_thenMatches() {
|
||||
String validInput = "Ab3;";
|
||||
assertTrue(Pattern
|
||||
.compile(regex)
|
||||
.matcher(validInput)
|
||||
.matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSingleRegex_whenMatchingWrongStrings_thenNotMatching() {
|
||||
String invalidInput = "Ab3";
|
||||
assertFalse(Pattern
|
||||
.compile(regex)
|
||||
.matcher(invalidInput)
|
||||
.matches());
|
||||
|
||||
invalidInput = "Ab;";
|
||||
assertFalse(Pattern
|
||||
.compile(regex)
|
||||
.matcher(invalidInput)
|
||||
.matches());
|
||||
|
||||
invalidInput = "A3;";
|
||||
assertFalse(Pattern
|
||||
.compile(regex)
|
||||
.matcher(invalidInput)
|
||||
.matches());
|
||||
|
||||
invalidInput = "b3;";
|
||||
assertFalse(Pattern
|
||||
.compile(regex)
|
||||
.matcher(invalidInput)
|
||||
.matches());
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.RegExUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class StringReplaceAndRemoveUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTestStrings_whenReplace_thenProcessedString() {
|
||||
|
||||
String master = "Hello World Baeldung!";
|
||||
String target = "Baeldung";
|
||||
String replacement = "Java";
|
||||
String processed = master.replace(target, replacement);
|
||||
assertTrue(processed.contains(replacement));
|
||||
assertFalse(processed.contains(target));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestStrings_whenReplaceAll_thenProcessedString() {
|
||||
|
||||
String master2 = "Welcome to Baeldung, Hello World Baeldung";
|
||||
String regexTarget = "(Baeldung)$";
|
||||
String replacement = "Java";
|
||||
String processed2 = master2.replaceAll(regexTarget, replacement);
|
||||
assertTrue(processed2.endsWith("Java"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestStrings_whenStringBuilderMethods_thenProcessedString() {
|
||||
|
||||
String master = "Hello World Baeldung!";
|
||||
String target = "Baeldung";
|
||||
String replacement = "Java";
|
||||
|
||||
int startIndex = master.indexOf(target);
|
||||
int stopIndex = startIndex + target.length();
|
||||
|
||||
StringBuilder builder = new StringBuilder(master);
|
||||
|
||||
builder.delete(startIndex, stopIndex);
|
||||
assertFalse(builder.toString()
|
||||
.contains(target));
|
||||
|
||||
builder.replace(startIndex, stopIndex, replacement);
|
||||
assertTrue(builder.toString()
|
||||
.contains(replacement));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestStrings_whenStringUtilsMethods_thenProcessedStrings() {
|
||||
|
||||
String master = "Hello World Baeldung!";
|
||||
String target = "Baeldung";
|
||||
String replacement = "Java";
|
||||
|
||||
String processed = StringUtils.replace(master, target, replacement);
|
||||
assertTrue(processed.contains(replacement));
|
||||
|
||||
String master2 = "Hello World Baeldung!";
|
||||
String target2 = "baeldung";
|
||||
String processed2 = StringUtils.replaceIgnoreCase(master2, target2, replacement);
|
||||
assertFalse(processed2.contains(target));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestStrings_whenReplaceExactWord_thenProcessedString() {
|
||||
String sentence = "A car is not the same as a carriage, and some planes can carry cars inside them!";
|
||||
String regexTarget = "\\bcar\\b";
|
||||
String exactWordReplaced = sentence.replaceAll(regexTarget, "truck");
|
||||
assertTrue("A truck is not the same as a carriage, and some planes can carry cars inside them!".equals(exactWordReplaced));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestStrings_whenReplaceExactWordUsingRegExUtilsMethod_thenProcessedString() {
|
||||
String sentence = "A car is not the same as a carriage, and some planes can carry cars inside them!";
|
||||
String regexTarget = "\\bcar\\b";
|
||||
String exactWordReplaced = RegExUtils.replaceAll(sentence, regexTarget, "truck");
|
||||
assertTrue("A truck is not the same as a carriage, and some planes can carry cars inside them!".equals(exactWordReplaced));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package com.baeldung.string.searching;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
|
||||
public class WordIndexerUnitTest {
|
||||
|
||||
String theString;
|
||||
WordIndexer wordIndexer;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() throws Exception {
|
||||
wordIndexer = new WordIndexer();
|
||||
|
||||
theString = "To be, or not to be: that is the question: "
|
||||
+ "Whether 'tis nobler in the mind to suffer "
|
||||
+ "The slings and arrows of outrageous fortune, "
|
||||
+ "Or to take arms against a sea of troubles, "
|
||||
+ "And by opposing end them? To die: to sleep; "
|
||||
+ "No more; and by a sleep to say we end "
|
||||
+ "The heart-ache and the thousand natural shocks "
|
||||
+ "That flesh is heir to, 'tis a consummation "
|
||||
+ "Devoutly to be wish'd. To die, to sleep; "
|
||||
+ "To sleep: perchance to dream: ay, there's the rub: "
|
||||
+ "For in that sleep of death what dreams may come,";
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
public void givenWord_whenSearching_thenFindAllIndexedLocations() {
|
||||
List<Integer> expectedResult = Arrays.asList(7, 122, 130, 221, 438);
|
||||
|
||||
List<Integer> actualResult = wordIndexer.findWord(theString, "or");
|
||||
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWordWithNoRepeatCharacters_whenImprovedSearching_thenFindAllIndexedLocations() {
|
||||
List<Integer> expectedResult = Arrays.asList(7, 122, 130, 221, 438);
|
||||
|
||||
List<Integer> actualResult = wordIndexer.findWordUpgrade(theString, "or");
|
||||
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenWord_whenSearching_thenFindAtEndOfString() {
|
||||
List<Integer> expectedResult = Arrays.asList(480);
|
||||
|
||||
List<Integer> actualResult = wordIndexer.findWordUpgrade(theString, "come,");
|
||||
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user