In this article, we will learn how to remove 1st and last character from a String using String‘s methods and StringBuffer/StringBuilder method in different ways
Before proceeding further, read the difference between String, StringBuffer and StringBuilder
Remove first & last character from a String :
- Using String.substring() method
- Using String.toCharArray() method
- Using StringBuffer.deleteCharAt() method
- Using StringBuilder.deleteCharAt() method
1. Using String.substring() method :
- String.substring(startIndex, endIndex) method returns partial string from the invoking string as per the specified start index-position and end index-position
- str.substring(1) returns substring starting from 1st index-position till end leaving the 1st character ‘B‘
- str.substring(0, str.length()-1) returns substring starting from 0th index-position till string length minus one leaving the last character ‘s‘
- Note: startIndex is inclusive and endIndex is exclusive
- Read Java – String substring(int beginIndex, int endIndex) method for more details and examples
RemoveFirstAndLastCharacterFromString1.java
package in.bench.resources.string.methods;
public class RemoveFirstAndLastCharacterFromString1 {
public static void main(String[] args) {
// test String
String str = "BenchResources";
System.out.println("Original String :- \n"
+ str);
// remove 1st char using String.substring() method
String firstCharRemovedStr = str.substring(1);
System.out.println("\nAfter removing First character from String '" + str + "' is :- \n"
+ firstCharRemovedStr);
// remove last char using String.substring() method
String lastCharRemovedStr = str.substring(0, str.length()-1);
System.out.print("\nAfter removing Last character in the String '" + str + "' is :- \n"
+ lastCharRemovedStr);
}
}
Output:
Original String :-
BenchResources
After removing First character from String 'BenchResources' is :-
enchResources
After removing Last character in the String 'BenchResources' is :-
BenchResource
2. Using String.toCharArray() method :
- String.toCharArray() method returns character[] Arrays for the invoking string
- Iterate through converted char[] Arrays from 1st index-position till end leaving the 1st character ‘B‘
- Iterate through converted char[] Arrays from 0th index-position till char Array length minus one leaving the last character ‘s‘
- Read Java – String toCharArray() method for more details and examples
RemoveFirstAndLastCharacterFromString2.java
package in.bench.resources.string.methods;
import java.util.Arrays;
public class RemoveFirstAndLastCharacterFromString2 {
public static void main(String[] args) {
// test String
String str = "BenchResources";
System.out.println("Original String :- \n"
+ str);
// convert String to char[] Arrays
char[] chArrays = str.toCharArray();
System.out.println("\nString to char[] Arrays :- \n"
+ Arrays.toString(chArrays));
// remove 1st char using index-position
System.out.println("\nAfter removing First character from String '" + str + "' is :- ");
for(int index = 1; index < chArrays.length; index++) {
System.out.print(str.charAt(index));
}
// remove last char using index-position
System.out.println("\n\nAfter removing Last character in the String '" + str + "' is :- ");
for(int index = 0; index < chArrays.length-1; index++) {
System.out.print(str.charAt(index));
}
}
}
Output:
Original String :-
BenchResources
String to char[] Arrays :-
[B, e, n, c, h, R, e, s, o, u, r, c, e, s]
After removing First character from String 'BenchResources' is :-
enchResources
After removing Last character in the String 'BenchResources' is :-
BenchResource
3. Using StringBuffer.deleteCharAt() method :
- StringBuffer.deleteCharAt(index) method deletes a single character at the specified index-position
- StringBuffer.deleteCharAt(0) deletes character at the 0th index-position i.e., 1st character and returns remaining string
- StringBuffer.deleteCharAt(str.length() – 1) deletes character at the last index-position i.e. last character and returns remaining string
- Note: difference between String and StringBuffer Java – String v/s StringBuffer
- Read Java – StringBuffer charAt(int index) method for more details and examples
RemoveFirstAndLastCharacterFromString3.java
package in.bench.resources.string.methods;
public class RemoveFirstAndLastCharacterFromString3 {
public static void main(String[] args) {
// test String
String str = "BenchResources";
System.out.println("Original String :- \n"
+ str);
// remove 1st char using index-position
StringBuffer stringBuffer1 = new StringBuffer(str);
String firstCharRemovedStr = stringBuffer1.deleteCharAt(0).toString();
System.out.println("\nAfter removing First character from String '" + str + "' is :- \n"
+ firstCharRemovedStr);
// remove last char using index-position
StringBuffer stringBuffer2 = new StringBuffer(str);
String lastCharRemovedStr = stringBuffer2.deleteCharAt(str.length()-1).toString();
System.out.print("\nAfter removing Last character from String '" + str + "' is :- \n"
+ lastCharRemovedStr);
}
}
Output:
Original String :-
BenchResources
After removing First character from String 'BenchResources' is :-
enchResources
After removing Last character from String 'BenchResources' is :-
BenchResource
4. Using StringBuilder.deleteCharAt() method :
- StringBuilder.deleteCharAt(index) method deletes a single character at the specified index-position
- StringBuilder.deleteCharAt(0) deletes character at the 0th index-position i.e., 1st character and returns remaining string
- StringBuilder.deleteCharAt(str.length() – 1) deletes character at the last index-position i.e. last character and returns remaining string
- Note: difference between StringBuilder and StringBuffer Java – StringBuffer v/s StringBuilder
- Read Java – StringBuilder charAt(int index) method for more details and examples
RemoveFirstAndLastCharacterFromString4.java
package in.bench.resources.string.methods;
public class RemoveFirstAndLastCharacterFromString4 {
public static void main(String[] args) {
// test String
String str = "BenchResources";
System.out.println("Original String :- \n"
+ str);
// remove 1st char using index-position
StringBuilder stringBuilder1 = new StringBuilder(str);
String firstCharRemovedStr = stringBuilder1.deleteCharAt(0).toString();
System.out.println("\nAfter removing First character from String '" + str + "' is :- \n"
+ firstCharRemovedStr);
// remove last char using index-position
StringBuilder stringBuilder2 = new StringBuilder(str);
String lastCharRemovedStr = stringBuilder2.deleteCharAt(str.length()-1).toString();
System.out.print("\nAfter removing Last character from String '" + str + "' is :- \n"
+ lastCharRemovedStr);
}
}
Output:
Original String :-
BenchResources
After removing First character from String 'BenchResources' is :-
enchResources
After removing Last character from String 'BenchResources' is :-
BenchResource
Related Article :
- Java – Remove duplicate characters from String in Java
- Java – How to UPPERCASE every duplicate character/occurrences in a String in Java
- Java – How to check whether 2 strings are Anagrams in Java ?
- Java – How to remove white-spaces in a String ?
- Java – How to swap 2 Strings without using Third variable ?
- Java – Count and print number of Vowels and Consonants in a String
- Java – Remove input-ted Character from a given String
- Java – How to check whether given String contains only Alphabets or Letters ?
- Java – How to check whether given String contains only Digits ?
- Java – How to check whether given String contains Alpha-numeric characters only ?
- Java – How to remove special characters from String ?
- Java – How to remove last comma (,) from String ?
- Java – How to count length of last word in a String ?
- Java – How to find first and last character in a String ?
- Java – How to remove first and last character from a String ?
References :
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Happy Coding !!
Happy Learning !!