how to remove character from stringbuilder in java

https:/‮w/‬ww.lautturi.com
how to remove character from stringbuilder in java

To remove a character from a StringBuilder object in Java, you can use the deleteCharAt method of the StringBuilder class. The deleteCharAt method removes the character at a specified index from the StringBuilder object.

Here is an example of how you can use the deleteCharAt method to remove a character from a StringBuilder object in Java:

StringBuilder sb = new StringBuilder("Hello, World!");
int index = sb.indexOf(",");
sb.deleteCharAt(index);

System.out.println(sb.toString());  // Outputs "Hello World!"

In this example, the indexOf method is used to find the index of the ',' character in the sbStringBuilder object. The deleteCharAt method is then used to remove the ',' character from the sb object.

You can use similar logic to remove any other character from the StringBuilder object, by specifying the index of the character in the deleteCharAt method. You can also use the delete method to remove a range of characters from the StringBuilder object, or the replace method to replace a range of characters with new characters.

Created Time:2017-11-01 20:42:59  Author:lautturi