Java
String Manipulation
Programming
Coding Tips
Java Methods

Java - removing first character of a string

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Removing the first character from a Java string is usually simple, but there are two different levels of "simple" here. If you mean the first UTF-16 code unit, substring(1) is usually enough. If you mean the first full Unicode character, especially when emoji or other supplementary characters are possible, you need to be more careful.

The Common Case: substring(1)

For ordinary ASCII or basic text input, the standard solution is substring(1).

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}

This works because Java strings are zero-indexed, so index 1 means "start from the second position and keep the rest."

For many business applications, that is exactly what you want. It is short, readable, and fast enough for normal usage.

Guard Against Empty or Null Strings

substring(1) throws StringIndexOutOfBoundsException on an empty string, and calling it on null throws NullPointerException. Guard those cases explicitly if they are possible.

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

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

Unicode Matters More Than It Looks

Java strings are stored as UTF-16. Some user-visible characters, such as many emoji, occupy two char values instead of one. In that case, substring(1) removes only half of the pair and leaves invalid text behind.

Example:

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 Unicode characters, remove the first code point instead of the first char.

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}

That version is safer for full Unicode text because it advances by one code point, not one UTF-16 unit.

Use StringBuilder for Repeated Edits

If you are repeatedly removing characters from the front during a larger transformation, a mutable structure may be easier to work with.

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

This is useful when you are already doing several edits. If you only need one removal, substring() is simpler.

Be aware that deleteCharAt(0) has the same Unicode limitation as substring(1) because it removes one char, not one code point.

Choose the Right Meaning of "Character"

There are really 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 text-processing problem and usually requires specialized libraries.

So the practical rule is:

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

Common Pitfalls

The most common mistake is calling substring(1) on an empty string. Always decide what the method should do for empty input and code that behavior explicitly.

Another frequent issue is ignoring Unicode. If your system accepts names, chat text, or emoji, removing one char may not remove one user-visible character.

Some developers also overuse StringBuilder for one-off cases. Since Java strings are immutable, you always get a new string anyway, but that does not mean you need a builder unless you are doing multiple mutations.

Finally, be careful with assumptions in validation code. Removing a leading sign from numeric text or a prefix from IDs may need extra checks before blindly dropping the first element.

Summary

  • 'substring(1) is the standard way to remove the first character in simple Java strings.'
  • Guard against null and empty input before calling it.
  • For Unicode-safe removal, skip one code point with offsetByCodePoints.
  • 'StringBuilder is useful when you are doing repeated mutations, not just one removal.'
  • Decide whether you mean first char or first full Unicode character before choosing the implementation.

Course illustration
Course illustration

All Rights Reserved.