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
- The idea is to use the substring() method of String class to remove first and the last character of a string.
- The substring(int beginIndex, int endIndex) method accepts two parameters, first is starting index, and the second is ending index.
- 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.
- Extract the substring excluding the first and last character using str.substring(1, str.length() – 1).
- 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)); } } |
eeksForGeek
Method 2: Using StringBuilder.deleteCharAt() method
- The idea is to use the deleteCharAt() method of StringBuilder class to remove first and the last character of a string.
- The deleteCharAt() method accepts a parameter as an index of the character you want to remove.
- Remove last character of a string using sb.deleteCharAt(str.length() – 1).
- Remove first character of a string using sb.deleteCharAt(0).
- 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)); } } |
eeksForGeek
Method 3: Using StringBuffer.delete() method
- The idea is to use the delete() method of StringBuffer class to remove first and the last character of a string.
- 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.
- Remove the last character of the string using sb.delete(str.length() – 1, str.length()).
- Remove the first character of the string using sb.delete(0, 1).
- 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)); } } |
eeksForGeek
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Remove the first and last character of each word in a string
- Remove the first and last occurrence of a given Character from a String
- Remove all occurrences of a character in a string
- Remove a character from a string to make it a palindrome
- Remove all occurrences of a character in a string | Recursive approach
- How to find the first and last character of a string in Java
- Swap the first and last character of a string in Java
- Java Program to get a character from a String
- Convert Character Array To String In Java
- Convert a String to Character array in Java
- Replace a character at a specific index in a String in Java
- Count occurrence of a given character in a string using Stream API in Java
- How to remove all non-alphanumeric characters from a string in Java
- Remove all non-alphabetical characters of a String in Java
- How to remove all white spaces from a String in Java?
- Remove Leading Zeros From String in Java
- Remove extra delimiter from a String in Java
- Trim (Remove leading and trailing spaces) a string in Java
- Replace every character of string by character whose ASCII value is K times more than it
- Find a string such that every character is lexicographically greater than its immediate next character
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

