How to remove in string in Java
In this tutorial, we are going to learn how to remove a substring and a character from a string in Java. In programming, strings are collections of characters that are used to represent text. They cannot have their contents altered after they are created because they are immutable. Numerous manipulation options are available for strings, including concatenation, slicing, and searching for particular substrings.
Example 1:
Input: String = “Hello World!” , Remove(str, 4)
Output: Hell World!
Explanation: In the input string "Hello World!", the character at index 4 is 'o. ' After applying the `Remove(str, 4)` operation, this character is removed, resulting in the output string "Hell World!" which demonstrates the removal of the character at the specified index, showcasing string manipulation in Java.
Example 2:
Input: String = “Hello World!!”, Substring = “World!!”
Output: Hello
Explanation: The program generates the output string "Hello" by removing the substring "World!!" from the input string "Hello World!!" given the input string and the substring "World!!".
Naive Approach
The naive approach iteratively creates new strings by concatenating substrings or by substituting empty strings for substrings, depending on the operations carried out. Although this method is simple, it might not be the most effective when working with large strings or frequently modifying them because it requires the creation of new strings, which can be computationally and spatially inefficient.
Here’s the implementation of a naive approach to remove a character and a substring from a string in Java:
FileName:StringRemoval.java
public class StringRemoval {
public static void main(String args[]) {
String originalString = "TutorialAndExample";
// Removing a character
int charIndex = 4;
String stringAfterCharRemoval = removeCharacter(originalString, charIndex);
System.out.println("String after removing character at index " + charIndex + ": " + stringAfterCharRemoval);
// Removing a substring
String substringToRemove = "And";
String stringAfterSubstringRemoval = originalString.replace(substringToRemove, "");
System.out.println("String after removing substring '" + substringToRemove + "': " + stringAfterSubstringRemoval);
}
// Method to remove a character at a given index
public static String removeCharacter(String str, int index) {
if (index < 0 || index >= str.length()) {
return str;
}
return str.substring(0, index) + str.substring(index + 1);
}
}
Output:
String after removing character at index 4: TutoialAndExample
String after removing substring 'And': TutorialExample
Complexity analysis: The first operation, removing a character, has a linear time complexity proportional to the string's length (n). The second operation, removing a substring, has a time complexity of O(n * m), where n is the original string and m is the substring to remove. The space complexity remains O(n) as new strings are created to store the modified strings.
Optimized Approach
In the optimized method, efficient string manipulation is achieved without needless string copying by utilizing `StringBuilder.` Because it avoids needless memory allocation and copying, this method performs better than the naive approach—especially for large strings or frequent manipulations.
FileName:StringRemoval1.java
public class StringRemoval {
public static void main(String args[]) {
String originalString = "TutorialAndExample";
// Removing a character
int charIndex = 4;
String stringAfterCharRemoval = removeCharacter(originalString, charIndex);
System.out.println("String after removing character at index " + charIndex + ": " + stringAfterCharRemoval);
// Removing a substring
String substringToRemove = "And";
String stringAfterSubstringRemoval = removeSubstring(originalString, substringToRemove);
System.out.println("String after removing substring '" + substringToRemove + "': " + stringAfterSubstringRemoval);
}
// Method to remove a character at a given index
public static String removeCharacter(String str, int index) {
if (index < 0 || index >= str.length()) {
return str;
}
StringBuilder sb = new StringBuilder(str);
sb.deleteCharAt(index);
return sb.toString();
}
// Method to remove a substring
public static String removeSubstring(String str, String substr) {
int index = str.indexOf(substr);
if (index == -1) {
return str;
}
StringBuilder sb = new StringBuilder(str);
sb.delete(index, index + substr.length());
return sb.toString();
}
}
Output:
String after removing character at index 4: TutoialAndExample
String after removing substring 'And': TutorialExample
Complexity analysis: The optimized approach for removing a character and a substring from a string in Java has a time complexity of O(n) due to the initial construction of the `StringBuilder.` The space complexity is O(n) due to the creation of a `StringBuilder` with the original string. The time complexity for removing a substring is O(n + m) due to the indexOf() method and the `delete()` method. The approach offers improved efficiency, especially for large strings or frequent manipulations, by avoiding unnecessary string copying.