Java

How to Remove the First Character of a String in Java

In Java, a string is represented using the String class found in java.lang package. It is the most frequently used class in the Java library. Every string we construct in Java is an object of the type String. One thing to keep in mind about string objects is that they will not be modified after their creation. However, there exist chances that you need to do other operations on a String, such as removing characters.

This tutorial will specifically discuss the method for removing the string’s first character in Java.

How to Remove the First Character of a String in Java?

In Java, you can remove the first character of a String by utilizing:

  • substring() method
  • StringBuilder.deleteCharAt() method
  • StringBuffer.delete() method

We will now check out each of the above-given methods one by one!

Method 1: Remove the First Character of a String Using substring() Method

To remove the string’s first character, use the Java String class “substring()” method. You can also use this method for deleting the string’s first and the last character at once. Since Strings are immutable, the resultant substring should be stored in a new String type variable.

Syntax

The general syntax of the substring() method is given as:

substring(int start, int end)

Here, the substring() method takes two parameters, “start” and “end”; the second parameter is optional. This method removes the starting and ending characters of the String and returns the resultant substring.

Though, if you intend to remove only the first character of a String, then you can pass the starting index as follows:

substring(int start)

Have a look at the below-given example to understand the stated concept.

Example

We will create a variable with the name “str” and print its value using the “System.out.println()” method:

String str= "Linuxhint" ;
System.out.println("Actual String: " + str);

Then, we will pass “1” as the starting index parameter to the “substring()” method. This operation returns a substring having all characters of the original String excluding the first one:

String newStr= str.substring(1);

Lastly, we will again utilize the System.out.println() method to display the resultant String:

System.out.println("Resultant String: " + newStr);

The output shows that the first letter of the “Linuxhint” String is successfully removed and the substring() method returned “inuxhint”:

Method 2: Remove the First Character of a String Using StringBuilder.deleteCharAt() Method

Another method to remove the string’s first character is the “deleteCharAt()” method. This method belongs to the “StringBuilder” class. Without creating new objects, StringBuilder allows the user to add or remove characters from strings because StringBuilder is mutable.

Syntax

The syntax of the “deleteCharAt()” method is given as below:

deleteCharAt(int index)

It accepts only one parameter and deletes the character present at the specified index.

Example

We will use the same string “str” that is created in the above-mentioned example. Now, we will create an object named “sbStr” of the StringBuilder class and pass “str” as a parameter:

StringBuilder sbStr = new StringBuilder(str);

Then, call the “deleteCharAt()” method and pass “0” as an argument to remove the first characters of the given string:

sbStr.deleteCharAt(0);

At last, print the substring using the “System.out.println()” method:

System.out.println("Resultant String by using String Builder: " + sbStr);

Output

Method 3: Remove the First Character of a String Using StringBuffer.delete() Method

The “delete()” method belongs to the “StringBuffer” class. This “StringBuffer.delete()” method is also used to remove the string’s first character in Java.

Syntax

The syntax of the method delete() of the StringBuffer class is:

delete(int startindex, int endindex)

It takes two parameters, “startindex” and “endindex”, and returns the substring after deleting the characters specified in the given range.

Example

First, we will create an object named “sbStr” of the StringBuffer class by passing a string “str” in it as an argument:

StringBuffer sbStr = new StringBuffer(str);

Then, we call the “delete()” method and pass “0” as the start index and “1” as the end index:

sbStr.delete(0,1);

Finally, print the resultant substring on console:

System.out.println("Resultant String by using String Buffer: " + sbStr);

As you can see, we have successfully removed the first letter of the “Linuxhint” string using the delete() method:

We have compiled all the essential instructions related to removing the first character of a string in Java.

Conclusion

For removing the string’s first character, you can use three methods: String.substring(), StringBuilder.deleteCharAt(), or StringBuffer.delete() method. String.substring() is significantly faster than other mentioned methods. It returns a new String with an updated starting and ending index. In this tutorial, we explained the methods for removing a string’s first character in Java.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.