Scarlett Ideas Back to catalog
How to Remove First and Last Character of String in Java - Example Tutorial
Aug 28, 2026 Scarlett

Java: Remove First Character from String

When working with strings in Java, there often arises the need to remove the first character. This can be achieved in several ways, each with its own merits and...

When working with strings in Java, there often arises the need to remove the first character. This can be achieved in several ways, each with its own merits and use cases. Let's delve into the methods to accomplish this, their implementations, and practical examples.

How to Remove First and Last Character of String in Java - Example Tutorial

Before we begin, let's start with a simple scenario. Assume we have a string 'Hello, World!' and we want to remove the first character 'H'.

How to remove duplicate characters from String in Java? [Solved]

Using String Methods

The String class in Java provides methods that allow manipulation of string data. Two of these methods can help us remove the first character – substring() and replaceFirst().

How to get first and last character of String in Java - Example

However, it's crucial to note that strings in Java are immutable. This means that any operation that seems to alter the string actually creates a new one. We'll explore this further in the following sections.

String Substring Method

How to Remove a character from String in Java ? Recursion Example [Solved]

The substring(int beginIndex) method returns a new string that starts from the specified index within this string. To remove the first character, we can start the substring from index 1.

Here's a code snippet demonstrating this:

```java String str = "Hello, World!"; String newStr = str.substring(1); System.out.println(newStr); // Output: "ello, World!" ```

String ReplaceFirst Method

How to Remove all adjacent duplicates characters from String in java? Example Tutorial

The replaceFirst(String regex, String replacement) method replaces the first occurrence of the specified regex pattern with the given replacement. To remove the first character, we can use a regular expression that matches exactly one character at the start of the string.

Here's an example:

```java String str = "Hello, World!"; String newStr = str.replaceFirst(".", ""); System.out.println(newStr); // Output: "ello, World!" ```

Using StringBuilder

a computer screen with the words strings on it and an image of a person sitting at a desk

As mentioned earlier, strings in Java are immutable. This can lead to increased memory usage and decreased performance when creating many new strings consecutively. To avoid this, we can use the StringBuilder class, which is mutable and more performant.

The deleteCharAt(int index) method in StringBuilder deletes the character at the specified position, allowing us to remove the first character efficiently.

Java Strings Cheat Sheet | Complete String Methods Guide for Beginners
Java Strings Made Easy | String Methods & StringBuilder Explained (Java Series #7)
Java string methods
Java String Methods
[Solved] How to reverse a String in place in Java? Example
Java Strings Explained | String Basics, Methods & Immutability | Day 19
How To Count Occurrences Of Each Character In String In Java
four different types of words that appear to be in the form of letters and numbers
7 Common Java Errors Beginners Make (And How to Fix Them) | Java Programming Tips

Here's an example:

```java StringBuilder sb = new StringBuilder("Hello, World!"); sb.deleteCharAt(0); System.out.println(sb.toString()); // Output: "ello, World!" ```

In conclusion, whether you're using a built-in string method or the mutable StringBuilder, removing the first character in a string in Java is straightforward, with various methods to suit different use cases. The best choice depends on your specific needs, such as the need for immutability or performance.

Happy coding, and remember to experiment with these methods to master them fully!

More shots

How to Remove First and Last Character of String in Java - Example Tutorial
How to Remove First and Last Character of String in Java - Example Tutorial
How to remove duplicate characters from String in Java? [Solved]
How to remove duplicate characters from String in Java? [Solved]
How to get first and last character of String in Java - Example
How to get first and last character of String in Java - Example
How to Remove a character from String in Java ? Recursion Example [Solved]
How to Remove a character from String in Java ? Recursion Example [Solved]
How to Remove all adjacent duplicates characters from String in java? Example Tutorial
How to Remove all adjacent duplicates characters from String in java? Example Tutorial
a computer screen with the words strings on it and an image of a person sitting at a desk
a computer screen with the words strings on it and an image of a person sitting at a desk
Java Strings Cheat Sheet  | Complete String Methods Guide for Beginners
Java Strings Cheat Sheet | Complete String Methods Guide for Beginners
Java Strings Made Easy | String Methods & StringBuilder Explained (Java Series #7)
Java Strings Made Easy | String Methods & StringBuilder Explained (Java Series #7)

Related topics