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:
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:
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:
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:
Lastly, we will again utilize the System.out.println() method to display the resultant String:
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:
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:
Then, call the “deleteCharAt()” method and pass “0” as an argument to remove the first characters of the given string:
At last, print the substring using the “System.out.println()” method:
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:
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:
Then, we call the “delete()” method and pass “0” as the start index and “1” as the end index:
Finally, print the resultant substring on console:
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.