Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Remove First and Last Character from String in Java

In this article, I will be sharing different ways to remove the first and last character from String in java. There are 2 ways to achieve the result.

1. Using substring() method
2. Using deleteCharAt(int index)

Let's dive deep into the topic.

Read Also: Difference between String, StringBuilder, and StringBuffer

1. Using substring(int beginIndex, int endIndex) method

substring() method is present in the String class. String class provides two overloaded substring methods but we will use the below method to remove first and the last character from the given String.

substring(int beginIndex, int endIndex)

The above method will create the substring starting from the beginIndex and extends to the character at index endIndex-1. In simple words, substring() will include beginIndex and extends till the end excluding the endIndex.

Since String is immutable i.e once created can not be changed or modified, substring() method will return a new String without first and last character. The length of the new substring() will be endIndex - beginIndex.

public class JavaHungry {
public static void main(String args[]) {
// Given String
String inputString = "JavaHungry";
// Calculate length of the given String
int length = inputString.length();
// Removing first and last character from String
System.out.println(inputString.substring(1,length-1));

// Above Code in One line
System.out.println("JavaHungry".substring(1,9));

}
}

Output:
avaHungr
avaHungr

2. Using deleteCharAt(int index)

deleteCharAt(int index) method is present in StringBuilder/StringBuffer class. We need to first convert String to StringBuilder/StringBuffer object.

Removing the first character from String using deleteCharAt(int index) method:

public class JavaHungry {
public static void main(String args[]) {
// Converting String to StringBuilder
StringBuilder str = new StringBuilder("JavaHungry");
// Removing first character
str.deleteCharAt(0);
// Converting StringBuilder to String
System.out.println(str.toString());
}
}

Output:
avaHungry

Removing the last character from String using deleteCharAt(int index) method:

public class JavaHungry {
public static void main(String args[]) {
// Converting String to StringBuilder
StringBuilder str = new StringBuilder("JavaHungry");
// Removing last character
str.deleteCharAt(str.length()-1);
// Converting StringBuilder to String
System.out.println(str.toString());
}
}

Output:
JavaHungr

Removing both the first and last character from String using deleteCharAt(int index) method:

public class JavaHungry {
public static void main(String args[]) {
//Converting String to StringBuilder
StringBuilder str = new StringBuilder("JavaHungry");
// Removing first character
str.deleteCharAt(0);
// Removing last character
str.deleteCharAt(str.length()-1);
//Converting StringBuilder to String
System.out.println(str.toString());
}
}

Output:
avaHungr

That's all for today regarding removing the first and last character from String in Java. If you know any other way to solve this problem or have any questions please mention in comments.


This post first appeared on Java Hungry, please read the originial post: here

Share the post

Remove First and Last Character from String in Java

×

Subscribe to Java Hungry

Get updates delivered right to your inbox!

Thank you for your subscription

×