In this article, we will learn how to find/get 1st and last character in a String using String‘s methods & StringBuffer/StringBuilder method in different ways
Before proceeding further, read the difference between String, StringBuffer and StringBuilder
Find first & last character in a String :
- Using String.charAt() method
- Using String.substring() method
- Using String.toCharArray() method
- Using StringBuffer.charAt() method
- Using StringBuilder.charAt() method
1. Using String.charAt() method :
- String.charAt(index) method returns character at the specified index-position
- 0th index-position returns 1st character
- String length minus one (i.e., str.length() – 1) index-position returns last character
- Read Java – String charAt(int index) method for more details and examples
FindFirstAndLastCharacterInString1.java
package in.bench.resources.string.methods;
public class FindFirstAndLastCharacterInString1 {
public static void main(String[] args) {
// test String
String str = "BenchResources";
System.out.println("Original String :- \n"
+ str);
// find 1st char using String.charAt() method
char firstChar = str.charAt(0);
System.out.println("\nFirst character in the String '" + str + "' is :- \n"
+ firstChar);
// find last char using String.charAt() method
char lastChar = str.charAt(str.length()-1);
System.out.print("\nLast character in the String '" + str + "' is :- \n"
+ lastChar);
}
}
Output:
Original String :-
BenchResources
First character in the String 'BenchResources' is :-
B
Last character in the String 'BenchResources' is :-
s
2. 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(0,1) returns 1st character
- str.substring(str.length()-1, str.length()) returns last character
- Note: startIndex is inclusive and endIndex is exclusive
- Read Java – String substring(int beginIndex, int endIndex) method for more details and examples
FindFirstAndLastCharacterInString2.java
package in.bench.resources.string.methods;
public class FindFirstAndLastCharacterInString2 {
public static void main(String[] args) {
// test String
String str = "BenchResources";
System.out.println("Original String :- \n"
+ str);
// find 1st char using String.substring() method
String firstChar = str.substring(0, 1);
System.out.println("\nFirst character in the String '" + str + "' is :- \n"
+ firstChar);
// find last char using String.substring() method
String lastChar = str.substring(str.length()-1, str.length());
System.out.print("\nLast character in the String '" + str + "' is :- \n"
+ lastChar);
}
}
Output:
Original String :-
BenchResources
First character in the String 'BenchResources' is :-
B
Last character in the String 'BenchResources' is :-
s
3. Using String.toCharArray() method :
- String.toCharArray() method returns character[] Arrays for the invoking string
- chArray[0] returns 1st character
- chArray[str.length()–1] returns last character
- Read Java – String toCharArray() method for more details and examples
FindFirstAndLastCharacterInString3.java
package in.bench.resources.string.methods;
import java.util.Arrays;
public class FindFirstAndLastCharacterInString3 {
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
char firstChar = chArrays[0];
System.out.println("\nFirst character in the String '" + str + "' is :- \n"
+ firstChar);
// remove last char using index-position
char lastChar = chArrays[str.length()-1];
System.out.print("\nLast character in the String '" + str + "' is :- \n"
+ lastChar);
}
}
Output:
Original String :-
BenchResources
String to char[] Arrays :-
[B, e, n, c, h, R, e, s, o, u, r, c, e, s]
First character in the String 'BenchResources' is :-
B
Last character in the String 'BenchResources' is :-
s
4. Using StringBuffer.charAt() method :
- StringBuffer.charAt(index) method returns character at the specified index-position
- 0th index-position returns 1st character
- String length minus one (i.e., str.length() – 1) index-position returns last character
- Note: difference between String and StringBuffer Java – String v/s StringBuffer
- Read Java – StringBuffer charAt(int index) method for more details and examples
FindFirstAndLastCharacterInString4.java
package in.bench.resources.string.methods;
public class FindFirstAndLastCharacterInString4 {
public static void main(String[] args) {
// test String
String str = "BenchResources";
System.out.println("Original String :- \n"
+ str);
// convert String to StringBuffer
StringBuffer stringBuffer = new StringBuffer(str);
// find 1st char using index-position
char firstChar = stringBuffer.charAt(0);
System.out.println("\nFirst character in the String '" + str + "' is :- \n"
+ firstChar);
// find last char using index-position
char lastChar = stringBuffer.charAt(str.length()-1);
System.out.print("\nLast character in the String '" + str + "' is :- \n"
+ lastChar);
}
}
Output:
Original String :-
BenchResources
First character in the String 'BenchResources' is :-
B
Last character in the String 'BenchResources' is :-
s
5. Using StringBuilder.charAt() method :
- StringBuilder.charAt(index) method returns character at the specified index-position
- 0th index-position returns 1st character
- String length minus one (i.e., str.length() – 1) index-position returns last character
- Note: difference between StringBuilder and StringBuffer Java – StringBuffer v/s StringBuilder
- Read Java – StringBuilder charAt(int index) method for more details and examples
FindFirstAndLastCharacterInString5.java
package in.bench.resources.string.methods;
public class FindFirstAndLastCharacterInString5 {
public static void main(String[] args) {
// test String
String str = "BenchResources";
System.out.println("Original String :- \n"
+ str);
// convert String to StringBuilder
StringBuilder stringBuilder = new StringBuilder(str);
// find 1st char using index-position
char firstChar = stringBuilder.charAt(0);
System.out.println("\nFirst character in the String '" + str + "' is :- \n"
+ firstChar);
// find last char using index-position
char lastChar = stringBuilder.charAt(str.length()-1);
System.out.print("\nLast character in the String '" + str + "' is :- \n"
+ lastChar);
}
}
Output:
Original String :-
BenchResources
First character in the String 'BenchResources' is :-
B
Last character in the String 'BenchResources' is :-
s
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 !!