Thursday, July 4, 2024
HomeData ModellingData Structure & AlgorithmRemove first and last character of a string in Java

Remove first and last character of a string in Java

Given string str, the task is to write Java Program to remove the first and the last character of the string and print the modified string.

Examples:

Input: str = “GeeksForGeeks”
Output: eeksForGeek
Explanation: The first character of the given string is ‘G’ and the last character of the given string is ‘s’. After removing the first and last character of a string, the string becomes “eeksForGeek”.

Input: str = “Java”
Output: av
Explanation: The first character of the given string is ‘J’ and the last character of the given string is ‘a’. After removing first and last character of a string, the string becomes “av”.

Method 1: Using String.substring() method

  1. The idea is to use the substring() method of String class to remove first and the last character of a string.
  2. The substring(int beginIndex, int endIndex) method accepts two parameters, first is starting index, and the second is ending index.
  3. The first character of a string is present at index zero and the last character of a string is present at index length of string – 1.
  4. Extract the substring excluding the first and last character using str.substring(1, str.length() – 1).
  5. Now, print the modified string.

Below is the implementation of the above approach:

Java




// Java program to remove the first and
// the last character of a string
 
class GFG {
 
    // Function to remove the first and
    // the last character of a string
    public static String
    removeFirstandLast(String str)
    {
 
        // Removing first and last character
        // of a string using substring() method
        str = str.substring(1, str.length() - 1);
 
        // Return the modified string
        return str;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        // Given String str
        String str = "GeeksForGeeks";
 
        // Print the modified string
        System.out.print(
            removeFirstandLast(str));
    }
}


Output:

eeksForGeek

Time Complexity: O(1)

Auxiliary Space: O(1)

Method 2: Using StringBuilder.deleteCharAt() method

  1. The idea is to use the deleteCharAt() method of StringBuilder class to remove first and the last character of a string.
  2. The deleteCharAt() method accepts a parameter as an index of the character you want to remove.
  3. Remove last character of a string using sb.deleteCharAt(str.length() – 1).
  4. Remove first character of a string using sb.deleteCharAt(0).
  5. Now, print the modified string.

Below is the implementation of the above approach:

Java




// Java program to remove the first and
// the last character of a string
 
class GFG {
 
    // Function to remove the first and
    // the last character of a string
    public static String
    removeFirstandLast(String str)
    {
 
        // Creating a StringBuilder object
        StringBuilder sb = new StringBuilder(str);
 
        // Removing the last character
        // of a string
        sb.deleteCharAt(str.length() - 1);
 
        // Removing the first character
        // of a string
        sb.deleteCharAt(0);
 
        // Converting StringBuilder into a string
        // and return the modified string
        return sb.toString();
    }
 
    // Driver Code
    public static void main(String args[])
    {
        // Given String str
        String str = "GeeksForGeeks";
 
        // Print the modified string
        System.out.println(
            removeFirstandLast(str));
    }
}


Output:

eeksForGeek

Time Complexity: O(1)

Auxiliary Space: O(n) because it using for creating StringBuilder sb, where n is length of string

Method 3: Using StringBuffer.delete() method

  1. The idea is to use the delete() method of StringBuffer class to remove first and the last character of a string.
  2. The delete(start_point, int end_point) method accepts two parameters, first is start_point, and the second is end_point and it returns the string after deleting the substring formed by the range mentioned in the parameters.
  3. Remove the last character of the string using sb.delete(str.length() – 1, str.length()).
  4. Remove the first character of the string using sb.delete(0, 1).
  5. Now, print the modified string.

Below is the implementation of the above approach:

Java




// Java program to remove the first and
// the last character of a string
 
class GFG {
 
    // Function to remove the first and
    // the last character of a string
    public static String
    removeFirstandLast(String str)
    {
 
        // Creating a StringBuffer object
        StringBuffer sb = new StringBuffer(str);
 
        // Removing the last character
        // of a string
        sb.delete(str.length() - 1, str.length());
 
        // Removing the first character
        // of a string
        sb.delete(0, 1);
 
        // Converting StringBuffer into
        // string & return modified string
        return sb.toString();
    }
 
    // Driver Code
    public static void main(String args[])
    {
        // Given String str
        String str = "GeeksForGeeks";
 
        // Print the modified string
        System.out.println(
            removeFirstandLast(str));
    }
}


Output:

eeksForGeek

Time Complexity: O(1)

Auxiliary Space: O(1) 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments