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 = “w3wiki”
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:
#include <iostream>
#include <string>
// Function to remove the first and last character of a
// string
std::string removeFirstAndLast(const std::string& s)
{
// Removing first and last character using string
// slicing
return s.substr(1, s.length() - 2);
}
int main()
{
// Given string s
std::string s = "w3wiki";
// Print the modified string
std::cout << removeFirstAndLast(s) << std::endl;
return 0;
}
// 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 = "w3wiki";
// Print the modified string
System.out.print(removeFirstandLast(str));
}
}
def remove_first_and_last(s):
# Removing first and last character
# of a string using slicing
return s[1:-1]
# Driver Code
if __name__ == "__main__":
# Given string s
s = "w3wiki"
# Print the modified string
print(remove_first_and_last(s))
function removeFirstAndLast(str) {
// Removing first and last character
// of a string using substring() method
return str.substring(1, str.length - 1);
}
// Driver Code
let str = "w3wiki";
// Print the modified string
console.log(removeFirstAndLast(str));
Output
eeksForGeek
Time Complexity: O(1)
Auxiliary Space: O(1)
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:
#include <iostream>
#include <string>
using namespace std;
// Function to remove the first and the last character of a
// string
string removeFirstAndLast(string str)
{
// Removing the last character of the string
str.pop_back();
// Removing the first character of the string
str.erase(str.begin());
// Return the modified string
return str;
}
// Driver code
int main()
{
// Given string str
string str = "w3wiki";
// Print the modified string
cout << removeFirstAndLast(str) << endl;
return 0;
}
// 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 = "w3wiki";
// Print the modified string
System.out.println(removeFirstandLast(str));
}
}
def remove_first_and_last(string):
# Remove the first and last character of the string
modified_string = string[1:-1]
return modified_string
# Driver code
if __name__ == "__main__":
# Given string
string = "w3wiki"
# Print the modified string
print(remove_first_and_last(string))
// Function to remove the first and the last character of a string
function removeFirstAndLast(str) {
// Check if the string has at least two characters
if (str.length >= 2) {
// Use substring to remove the first and last characters
return str.substring(1, str.length - 1);
} else {
// Return empty string if the string has less than two characters
return "";
}
}
// Driver Code
// Given String str
var str = "w3wiki";
// Print the modified string
console.log(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
- 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:
#include <iostream>
#include <string>
using namespace std;
// Function to remove the first and
// the last character of a string
string removeFirstandLast(string str)
{
// Removing the last character
// of a string
str.erase(str.length() - 1, 1);
// Removing the first character
// of a string
str.erase(0, 1);
// Return modified string
return str;
}
// Driver Code
int main()
{
// Given String str
string str = "w3wiki";
// Print the modified string
cout << removeFirstandLast(str);
return 0;
}
// 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 = "w3wiki";
// Print the modified string
System.out.println(removeFirstandLast(str));
}
}
# Function to remove the first and the last character of a string
def remove_first_and_last(s):
# Creating a new string with the first and last character removed
# s[1:-1] means slicing the string from the second character to the second-to-last character
modified_str = s[1:-1]
return modified_str
# Driver code
if __name__ == "__main__":
# Given string s
s = "w3wiki"
# Print the modified string
print(remove_first_and_last(s))
// Function to remove the first and the last character of a string
function removeFirstandLast(str) {
// Removing the last character of the string
str = str.substring(0, str.length - 1);
// Removing the first character of the string
str = str.substring(1);
// Return the modified string
return str;
}
// Given string
let str = "w3wiki";
// Print the modified string
console.log(removeFirstandLast(str));
Output
eeksForGeek
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 4: Using Regular Expression
- The idea is used to Regular Expression for removing the first and last character from the string.
- First we pass the regex in the Patter.compile() method.
- Then, we use pattern object with pattern.mather() method
- Then, we remove the first and last character of string using Regex store the result in result variable
- Then, Print the modified string.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
String str = "w3wiki";
// Regular expression pattern to match the first and last characters
Pattern pattern = Pattern.compile(".(.*).");
Matcher matcher = pattern.matcher(str);
if (matcher.matches()) {
String result = matcher.group(1);
System.out.println(result);
}
}
}
Output
eeksforGeek
Time Complexity: O(n)
Space Complexity: O(n)
Contact Us