Java
String Manipulation
Programming
Coding Tips
Java Methods

Java: remove the first character of a string

Codemia Knowledge Hub ·

Use value.substring(1). It returns everything from index 1 onward, which drops the first UTF-16 char. If the string can be empty or null, check first, and if it can start with an emoji or another supplementary character, skip one code point with offsetByCodePoints instead.

substring(1) for ordinary text

java
1public class Main {
2    public static void main(String[] args) {
3        String value = "Hello";
4        String result = value.substring(1);
5        System.out.println(result); // ello
6    }
7}

Java strings are zero-indexed, so index 1 means "start from the second position and keep the rest". For most business code this is all you need: short, readable and fast enough.

Handling null and empty strings

substring(1) throws StringIndexOutOfBoundsException on an empty string, and calling it on null throws NullPointerException. Decide what the method should return in those cases and code it explicitly:

java
1public static String removeFirstChar(String value) {
2    if (value == null || value.isEmpty()) {
3        return value;
4    }
5    return value.substring(1);
6}

That check makes the method safe for external input, CSV data, request parameters and other unreliable sources.

Removing the first code point for emoji and Unicode

Java strings are stored as UTF-16, and some user-visible characters, including many emoji, take two char values. substring(1) then removes only half of the pair and leaves invalid text:

java
1public class Main {
2    public static void main(String[] args) {
3        String value = "😀Hello";
4        String broken = value.substring(1);
5        System.out.println(broken);
6    }
7}

If your input can contain supplementary characters (names, chat text, emoji), remove the first code point instead:

java
1public static String removeFirstCodePoint(String value) {
2    if (value == null || value.isEmpty()) {
3        return value;
4    }
5
6    int start = value.offsetByCodePoints(0, 1);
7    return value.substring(start);
8}

This advances by one code point rather than one UTF-16 unit.

StringBuilder.deleteCharAt(0) for repeated edits

When you are making several edits as part of a larger transformation, a mutable builder can be easier:

java
1StringBuilder builder = new StringBuilder("Hello");
2builder.deleteCharAt(0);
3
4System.out.println(builder); // ello

For a single removal, substring() is simpler. Strings are immutable, so you get a new string either way, and a builder only pays off for multiple mutations. deleteCharAt(0) has the same Unicode limitation as substring(1), because it removes one char, not one code point.

Which "first character" you mean

There are three common interpretations:

  • first char
  • first Unicode code point
  • first grapheme cluster as seen by the user

For most Java code, choosing between char and code point is enough. Full grapheme-cluster handling is a more advanced problem that usually needs specialized libraries. In practice:

  • use substring(1) for simple text
  • use offsetByCodePoints when Unicode correctness matters

In validation code, such as stripping a leading sign from numeric text or a prefix from IDs, check that the first character is what you expect before dropping it.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms