Removing the first character from a string in Java
In this tutorial, we are going to learn about how to remove the first character from a string in Java.
Removing the first character
To remove the first character from a string, we can use the built-in substring() method in Java.
Here is an example, that removes the first character h
from the following string.
String str = "hello";
String result = str.substring(1);
System.out.println(str.substring(1));
Output:
ello
In the example above, we have passed 1
as argument to the substring() method. so it begins the extraction at index position 1
and extracts to the end of a string.
Note: The substring()
method doesn’t mutates the original string, instead of it creates a new one with the extracted characters.