Given a string str, the task is to print the first and the last character of the string.
Examples:
Input: str = “GeeksForGeeks”
Output:
First: G
Last: s
Explanation: The first character of the given string is ‘G’ and the last character of given string is ‘s’.
Input: str = “Java”
Output:
First: J
Last: a
Explanation: The first character of given string is ‘J’ and the last character of given string is ‘a’.
Method 1: Using String.charAt() method
- The idea is to use charAt() method of String class to find the first and last character in a string.
- The charAt() method accepts a parameter as an index of the character to be returned.
- The first character in a string is present at index zero and the last character in a string is present at index length of string-1 .
- Now, print the first and the last character of the string.
Below is the implementation of the above approach:
Java
// Java program to find first // and last character of a string class GFG { // Function to print first and last // character of a string public static void firstAndLastCharacter(String str) { // Finding string length int n = str.length(); // First character of a string char first = str.charAt(0); // Last character of a string char last = str.charAt(n - 1); // Printing first and last // character of a string System.out.println("First: " + first); System.out.println("Last: " + last); } // Driver Code public static void main(String args[]) { // Given string str String str = "GeeksForGeeks"; // Function Call firstAndLastCharacter(str); } } |
First: G Last: s
Method 2: Using String.toCharArray() method
- The idea is to first convert the given string into a character array using toCharArray() method of String class, then find the first and last character of a string and print it.
Below is the implementation of the above approach:
Java
// Java program to find first // and last character of a string class GFG { // Function to print first and last // character of a string public static void firstAndLastCharacter(String str) { // Converting a string into // a character array char[] charArray = str.toCharArray(); // Finding the length of // character array int n = charArray.length; // First character of a string char first = charArray[0]; // Last character of a string char last = charArray[n - 1]; // Printing first and last // character of a string System.out.println("First: " + first); System.out.println("Last: " + last); } // Driver Code public static void main(String args[]) { // Given string str String str = "GeeksForGeeks"; // Function Call firstAndLastCharacter(str); } } |
First: G Last: s
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find a string such that every character is lexicographically greater than its immediate next character
- Find the character in first string that is present at minimum index in second string
- Queries to find the first non-repeating character in the sub-string of a string
- Queries to find the last non-repeating character in the sub-string of a given string
- Find the first repeated character in a string
- Find one extra character in a string
- Find k'th character of decrypted string | Set 1
- Find k-th character of decrypted string | Set - 2
- Find the last non repeating character in string
- Find the Nth occurrence of a character in the given String
- Given a string, find its first non-repeating character
- Find last index of a character in a string
- Find repeated character present first in a string
- Program to find the kth character after decrypting a string
- Java Program to get a character from a String
- Remove first and last character of a string in Java
- Find first non-repeating character in a given string using Linked List
- Find the largest Alphabetic character present in the string
- Find the last remaining Character in the Binary String according to the given conditions
- Find the character made by adding all the characters of the given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

