How to Remove the First Character in a String in Java
Suppose we want to remove the first character from the string
coffee to create the word
String str = "coffee"; // We want "offee"
In Java, we can use the
substring() function to obtain any substring of a string.
As explained in the docs, we have two ways of using this method:
substring(int beginIndex) substring(int beginIndex, int endIndex)
If we supply just one parameter, this method will return the substring from
beginIndex (inclusive) to the end of the string.
str.substring(1); // "offee"
More Java Articles
- How to Remove the Last Character in a String in Java
- How to Split a String Over Only the First Occurrence in Java
- How to Get the Substring Before a Character in Java
- How to Pass Dynamic Number of Parameters in Java
- How to Use Generics with Abstract Classes in Java For Different Parameter Types
- How to Replace the String Between Two Characters in Java
- How to Print Elements of a List in Java Without Loops
- How to Ignore Test Cases in JUnit
- How to Add Multiple Elements to List At Once in Java