Java Program to remove a character at a specified position


To remove a character at a specified position, use the following logic −

Let’s say the following is our string.

String str = "The Haunting of Hill House!";

We want to remove a character at position 7. For that, use the below logic.

// removing character at position 7
int pos = 7;
String res = str.substring(0, pos) + str.substring(pos + 1);

The following is the complete example with output.

Example

 Live Demo

public class Demo {
    public static void main(String[] args) {
       String str = "The Haunting of Hill House!";
       System.out.println("String: "+str);
       // removing character at position 7
       int pos = 7;
       String res = str.substring(0, pos) + str.substring(pos + 1);
       System.out.println("String after removing a character: "+res);
    }
}

Output

String: The Haunting of Hill House!
String after removing a character: The Hauting of Hill House!

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 26-Jun-2020

616 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements