How to remove the first character of a string in Java

How to remove the first character of a string in Java:

In this post, we will learn how to remove the first character of a string in Java. Strings are immutable in Java. We can’t directly modify a string. If we modify a string like adding a new character, deleting a character etc., it creates another string.

I will show you two different ways to remove the first character of a string in Java.

Method 1: By using substring() method:

The substring() method is defined in the String class. This method is used to get a substring from a string.

This method is defined as like below:

public String substring(int start, int end)

It takes two parameters:

  • start is the start index in the string for the substring.
  • end is the end index in the string for the substring. This is an optional value. If you don’t provide this value, it will pick to the end of the string.

start is inclusive and end is exclusive.

It might throw StringIndexOutOfBoundsException if:

  • If the value of start is a negative number.
  • If end is smaller than start
  • If start or end is bigger than the length of the string.

It return a new string, i.e. the sub-string created by using the indices.

Method 1: Remove the first character of a string by using substring:

We can use the substring method to remove the first character of a string. If we pass 1 as start and no value for end, it will return return one string starting from the index 1 to the end of the string. This is the required string, i.e. string without the first character.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String givenString, newString;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a string: ");
        givenString = sc.nextLine();

        newString = givenString.substring(1);

        System.out.println("You have entered: "+givenString);
        System.out.println("String after the first character is removed: "+newString);
    }
}

Here,

  • givenString and newString are two String variables to hold the user-input string and the newly created string.
  • sc is a Scanner object to read user inputs.
  • It asks the user to enter a string, reads it and stores the values in givenString.
  • It calls substring on givenString and the returned value is stored in newString.
  • The last two lines are printing the givenString and newString values.

If you run the above program, it will print output as like below:

Enter a string: 
Hello World
You have entered: Hello World
String after the first character is removed: ello World

Method 2: Remove the first character of a string by converting it to a StringBuilder:

StringBuilder class has many advantages over String. Unlike String, StringBuilder objects are mutable. We can add/remove characters of a StringBuilder object without creating any new objects.

StringBuilder objects can be created by passing a string to its constructor. Similarly, we can also convert a StringBuilder object to string by using the toString() method.

So, we need to convert a string to StringBuilder, delete the first character and convert it back to a string.

We can use the below method to delete the first character of a StringBuilder:

public StringBuilder deleteCharAt(int index)

This method deletes a character at the given index. It returns a StringBuilder, i.e. the updated StringBuilder object.

We can use this method to remove the first character and convert the StringBuilder back to a string.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String givenString, newString;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a string: ");
        givenString = sc.nextLine();

        newString = new StringBuilder(givenString).deleteCharAt(0).toString();

        System.out.println("You have entered: "+givenString);
        System.out.println("String after the first character is removed: "+newString);
    }
}

If you run this program, it will give similar output.

Java Remove first string character

You might also like: