Remove first and last character of a string in Java
Given a string str, the task is to write the 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 and last characters of the given string are 'G' and 's' respectively. After removing both the characters, the string will be "eeksForGeek".Input: str = "Java"
Output: "av"
Explanation: The first and last characters of the given string are 'J' and 'a' respectively. After removing both the characters, the string will be "av".
Table of Content
[Approach - 1] Using string.substring() Method - O(n) Time and O(1) Space
The idea is to use the String.substring() method of string class to remove the first and the last character of a string.
- This method accepts two parameters, the start and end index of the substring, and extracts the substring from index start to end - 1.
- We are required to remove the characters at index 0 and n - 1, thus we will call the method with parameters 1 and n - 1.
// 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
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;
}
public static void main(String args[]) {
// Given String str
String str = "GeeksForGeeks";
// Print the modified string
System.out.print(removeFirstandLast(str));
}
}
Output
eeksForGeek
[Approach - 2] Using StringBuilder.deleteChatAt() Method - O(n) Time and O(n) Space
The idea is to use the deleteCharAt() method of StringBuilder class to remove first and the last character of a string. To do so, firstly create a StringBuilder object sb for the given string. Remove the first and last character using sb.deleteCharAt(0) and sb.deleteCharAt(str.length() - 1) respectively.
// 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
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();
}
public static void main(String args[]) {
// Given String str
String str = "GeeksForGeeks";
// Print the modified string
System.out.println(removeFirstandLast(str));
}
}
Output
eeksForGeek
[Approach - 3] Using StringBuffer.delete() Method - O(n) Time and O(n) Space
The idea is to use the delete() method of StringBuffer class to remove first and the last character of a string.
- To do so, firstly create the StringBuffer object for the given string. This method accepts two parameters, the start and end index of the string to delete.
- Remove the first and the last character using sb.delete(0, 1) and sb.delete(str.length() - 1, str.length()) respectively.
// 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
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();
}
public static void main(String args[]) {
// Given String str
String str = "GeeksForGeeks";
// Print the modified string
System.out.println(removeFirstandLast(str));
}
}
Output
eeksForGeek
[Approach - 4] Using Regular Expression - O(n) Time and O(1) Space
The idea is used to Regular Expression for removing the first and last character from the string.
- To do so, first pass the regex in the pattern.compile() method. Then use pattern object with pattern.matcher() method.
- At last, remove the first and last character of string using Regex store the result in result variable
// Java program to remove the first and
// the last character of a string
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GfG {
// Function to remove the first and
// the last character of a string
static String removeFirstandLast(String str) {
// Regular expression pattern to match the first and
// last characters
Pattern pattern = Pattern.compile(".(.*).");
Matcher matcher = pattern.matcher(str);
if (matcher.matches()) {
return matcher.group(1);
}
return str;
}
public static void main(String[] args) {
String str = "GeeksforGeeks";
// Print the modified string
System.out.println(removeFirstandLast(str));
}
}
Output
eeksforGeek