YouTip LogoYouTip

String Removing Char

## Java String: How to Remove a Character from a String In Java, `String` objects are immutable, meaning their values cannot be changed after they are created. Therefore, to "remove" a character from a string, you must construct a new string that excludes the target character. This tutorial demonstrates how to remove a character at a specific index using the `substring()` method, along with alternative modern approaches. --- ## Method 1: Using `String.substring()` The most common and lightweight way to remove a character at a specific index is by splitting the string into two partsβ€”before and after the target indexβ€”and then concatenating them back together. ### Syntax and Logic ```java public static String removeCharAt(String s, int pos) { return s.substring(0, pos) + s.substring(pos + 1); } ``` * **`s.substring(0, pos)`**: Extracts the substring starting from index `0` up to (but not including) the target index `pos`. * **`s.substring(pos + 1)`**: Extracts the substring starting from index `pos + 1` to the end of the string, effectively skipping the character at index `pos`. ### Complete Code Example Below is a complete Java program demonstrating this implementation: ```java public class Main { public static void main(String[] args) { String str = "this is Java"; // Remove the character at index 3 (the letter 's' in "this") System.out.println("Original String: " + str); System.out.println("Modified String: " + removeCharAt(str, 3)); } /** * Removes a character at a specified index from a string. * * @param s The original string * @param pos The index of the character to be removed * @return A new string with the character removed */ public static String removeCharAt(String s, int pos) { return s.substring(0, pos) + s.substring(pos + 1); } } ``` ### Output ```text Original String: this is Java Modified String: thi is Java ``` --- ## Method 2: Using `StringBuilder` (Recommended for Performance) If you are performing multiple string manipulation operations, using `StringBuilder` is highly recommended. Unlike `String`, `StringBuilder` is mutable, which avoids creating unnecessary intermediate string objects in memory. ### Code Example ```java public class Main { public static void main(String[] args) { String str = "this is Java"; System.out.println(removeCharAtUsingBuilder(str, 3)); } public static String removeCharAtUsingBuilder(String s, int pos) { if (s == null) { return null; } StringBuilder sb = new StringBuilder(s); sb.deleteCharAt(pos); return sb.toString(); } } ``` --- ## Important Considerations 1. **IndexOutOfBoundsException**: Both `substring()` and `StringBuilder.deleteCharAt()` will throw an `IndexOutOfBoundsException` if the specified index `pos` is negative, or greater than or equal to the length of the string. Always validate the input index in production environments: ```java if (s == null || pos < 0 || pos >= s.length()) { return s; // Or throw an IllegalArgumentException } ``` 2. **Immutability**: Remember that the original string remains unchanged. You must assign the returned value to a new variable or overwrite the existing one to save the changes: ```java String original = "Hello"; String modified = removeCharAt(original, 1); // "Hllo" ```
← String SplitString Last Occurance β†’