Currently, I am working on a project that requires to delete a char at a set position in a string. Is there a simple way to do this?
Use a StringBuilder, which has the method deleteCharAt()
. Then, you can just use stringBuilder.toString()
to get your string.
EDIT, here's an example:
public static void main(String[] args) {
String string = "bla*h";
StringBuilder sb = new StringBuilder(string);
sb.deleteCharAt(3);
// Prints out "blah"
System.out.println(sb.toString());
}
Strings are immutable ! But to accomplish your task, you can
For example, let's say you have to remove the third character:
String input = "Hello, World";
String first = input.substring(0, 3);
String second = input.substring(4);
String result = first + second;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With