Remove First and Last Character from String in Java

1. Introduction

Removing the first and last characters from a string in Java is a common operation that can be needed for various reasons, such as parsing inputs, data preprocessing, or other types of string manipulation. Java offers multiple ways to achieve this, each with its own advantages. This blog post will explore different methods to remove the first and last characters from a string in Java, including using substring, StringBuilder, and Java 8 features.

2. Program Steps

1. Use the substring method of the String class.

2. Use StringBuilder to delete characters at specific positions.

3. Use Java 8 Stream API for a more functional approach.

3. Code Program

public class RemoveFirstLastCharExample {
    public static void main(String[] args) {
        String originalString = "Hello, World!";

        // Method 1: Using substring
        String usingSubstring = originalString.substring(1, originalString.length() - 1);

        // Method 2: Using StringBuilder
        StringBuilder sb = new StringBuilder(originalString);
        sb.deleteCharAt(0); // Removes the first character
        sb.deleteCharAt(sb.length() - 1); // Removes the last character (now at length - 1 after the first removal)
        String usingStringBuilder = sb.toString();

        // Method 3: Using Java 8 Stream API (Assuming the task is to remove first and last if string length > 2)
        String usingStream = originalString.chars() // Creates an IntStream
                .skip(1) // Skips the first character
                .limit(originalString.length() - 2) // Limits the stream to length - 2 to remove the last character
                .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)

        System.out.println("Original: " + originalString);
        System.out.println("Using substring: " + usingSubstring);
        System.out.println("Using StringBuilder: " + usingStringBuilder);
        System.out.println("Using Stream: " + usingStream);


Original: Hello, World!
Using substring: ello, World
Using StringBuilder: ello, World!
Using Stream: ello, World


1. Using substring: This method is straightforward and utilizes substring to create a new string starting from the second character up to the second-last character of the original string. It is concise but creates a new string object.

2. Using StringBuilder: StringBuilder is more flexible and allows for in-place modification of the string. Characters are removed using deleteCharAt, which adjusts the string's length dynamically. This method is efficient for longer strings or when multiple manipulations are performed.

3. Using Java 8 Stream API: The Stream API provides a functional approach, turning the string into a stream of characters, then skipping the first and truncating the stream before the last character, and finally collecting the stream back into a string. This method offers a high level of expressiveness and is useful in more complex scenarios or when working within a functional programming paradigm.

4. Each method has its use cases, and the choice depends on the specific requirements, such as performance considerations and code readability. This example demonstrates the versatility of Java in string manipulation tasks.