In this post, I am going to explain how to remove single character at given index from a string. Strings in Java are immutable, so once a string is defined, you can’t change the value. Possible way is to create new string that do not contain the character at given index from original string.
Multiple approaches to remove single character at given index from a string.
Approach 1: Traverse the string character by character, form a new string by skipping the character at specific index.
SkipCharDemo1.java
package com.sample.app.strings;
public class SkipCharDemo1 {
private static String skipCharAtIndex(String str, int index) {
if (str == null || index < 0 || index > str.length()) {
return str;
}
char[] chars = str.toCharArray();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < chars.length; i++) {
if (i == index) {
continue;
}
stringBuilder.append(chars[i]);
}
return stringBuilder.toString();
}
public static void main(String args[]) {
String str = "Hello";
System.out.println("str -> " + str);
for (int i = 0; i < str.length(); i++) {
String result = skipCharAtIndex(str, i);
System.out.println("Skipped ch at index " + i + ", result " + result);
}
}
}
Output
str -> Hello Skipped ch at index 0, result ello Skipped ch at index 1, result Hllo Skipped ch at index 2, result Helo Skipped ch at index 3, result Helo Skipped ch at index 4, result Hell
Approach 2: Get StringBuilder instance from the string, remove the character at given index using deleteCharAt method.
SkipCharDemo2.java
package com.sample.app.strings;
public class SkipCharDemo2 {
private static String skipCharAtIndex(String str, int index) {
if (str == null || index < 0 || index > str.length()) {
return str;
}
StringBuilder stringBuilder = new StringBuilder(str);
return stringBuilder.deleteCharAt(index).toString();
}
public static void main(String args[]) {
String str = "Hello";
System.out.println("str -> " + str);
for (int i = 0; i < str.length(); i++) {
String result = skipCharAtIndex(str, i);
System.out.println("Skipped ch at index " + i + ", result " + result);
}
}
}
Output
str -> Hello Skipped ch at index 0, result ello Skipped ch at index 1, result Hllo Skipped ch at index 2, result Helo Skipped ch at index 3, result Helo Skipped ch at index 4, result Hell
Approach 3: using substring method.
SkipCharDemo3.java
package com.sample.app.strings;
public class SkipCharDemo3 {
private static String skipCharAtIndex(String str, int index) {
if (str == null || index < 0 || index > str.length()) {
return str;
}
return str.substring(0, index) + str.substring(index + 1);
}
public static void main(String args[]) {
String str = "Hello";
System.out.println("str -> " + str);
for (int i = 0; i < str.length(); i++) {
String result = skipCharAtIndex(str, i);
System.out.println("Skipped ch at index " + i + ", result " + result);
}
}
}
Output
str -> Hello Skipped ch at index 0, result ello Skipped ch at index 1, result Hllo Skipped ch at index 2, result Helo Skipped ch at index 3, result Helo Skipped ch at index 4, result Hell
You may like
Java: Get the index of first occurrence of a substring
Java: Get the index of last occurrence of a substring
Java: Get all the indexes of occurrence of a substring
No comments:
Post a Comment