Java Program to get a character from a String
Given a String str, the task is to get a specific character from that String at a specific index.
Examples:
Input: str = "Geeks", index = 2 Output: e Input: str = "GeeksForGeeks", index = 5 Output: F
Below are various ways to do so:
- Using String.charAt() method:
- Get the string and the index
- Get the specific character using String.charAt(index) method.
- Return the specific character.
Below is the implementation of the above approach:
// Java program to get a specific character// from a given String at a specific indexclassGFG {// Function to get the specific characterpublicstaticchargetCharFromString(String str,intindex){returnstr.charAt(index);}// Driver codepublicstaticvoidmain(String[] args){// Get the StringString str ="GeeksForGeeks";// Get the indexintindex =5;// Get the specific charactercharch = getCharFromString(str, index);System.out.println("Character from "+ str+" at index "+ index+" is "+ ch);}}chevron_rightfilter_noneOutput:Character from GeeksForGeeks at index 5 is F
- Using String.toCharArray() method:
- Get the string and the index
- Convert the String into Character array using String.toCharArray() method.
- Get the specific character at the specific index of the character array.
- Return the specific character.
Below is the implementation of the above approach:
// Java program to get a specific character// from a given String at a specific indexclassGFG {// Function to get the specific characterpublicstaticchargetCharFromString(String str,intindex){returnstr.toCharArray()[index];}// Driver codepublicstaticvoidmain(String[] args){// Get the StringString str ="GeeksForGeeks";// Get the indexintindex =5;// Get the specific charactercharch = getCharFromString(str, index);System.out.println("Character from "+ str+" at index "+ index+" is "+ ch);}}chevron_rightfilter_noneOutput:Character from GeeksForGeeks at index 5 is F
- Using Java 8 Streams:
- Get the string and the index
- Convert String into IntStream using String.chars() method.
- Convert IntStream into Stream
using IntStream.mapToObj() method. - Convert Stream
into Character[] using toArray() method. - Get the element at the specific index from this character array.
- Return the specific character.
Below is the implementation of the above approach:
// Java program to get a specific character// from a given String at a specific indexclassGFG {// Function to get the specific characterpublicstaticchargetCharFromString(String str,intindex){returnstr// Convert String into IntStream.chars()// Convert IntStream into Stream<Character>.mapToObj(ch -> (char)ch)// Convert Stream<Character> into Character[]// and get the element at the specific index.toArray(Character[] ::new)[index];}// Driver codepublicstaticvoidmain(String[] args){// Get the StringString str ="GeeksForGeeks";// Get the indexintindex =5;// Get the specific charactercharch = getCharFromString(str, index);System.out.println("Character from "+ str+" at index "+ index+" is "+ ch);}}chevron_rightfilter_noneOutput:Character from GeeksForGeeks at index 5 is F
- Using String.codePointAt() method:
- Get the string and the index
- Get the specific character ASCII value at the specific index using String.codePointAt() method.
- Convert this ASCII value into character using type casting.
- Return the specific character.
Below is the implementation of the above approach:
// Java program to get a specific character// from a given String at a specific indexclassGFG {// Function to get the specific characterpublicstaticchargetCharFromString(String str,intindex){return(char)str.codePointAt(index);}// Driver codepublicstaticvoidmain(String[] args){// Get the StringString str ="GeeksForGeeks";// Get the indexintindex =5;// Get the specific charactercharch = getCharFromString(str, index);System.out.println("Character from "+ str+" at index "+ index+" is "+ ch);}}chevron_rightfilter_noneOutput:Character from GeeksForGeeks at index 5 is F
- Using String.getChars() method:
- Get the string and the index
- Create an empty char array of size 1
- Copy the element at specific index from String into the char[] using String.getChars() method.
- Get the specific character at the index 0 of the character array.
- Return the specific character.
Below is the implementation of the above approach:
// Java program to get a specific character// from a given String at a specific indexclassGFG {// Function to get the specific characterpublicstaticchargetCharFromString(String str,intindex){// Create a character array of size 1char[] singleCharArray =newchar[1];// Get the specific character from the String// into the char[] at index 0str.getChars(index, index +1, singleCharArray,0);// Return the specific character// present at index 0 in char[]returnsingleCharArray[0];}// Driver codepublicstaticvoidmain(String[] args){// Get the StringString str ="GeeksForGeeks";// Get the indexintindex =5;// Get the specific charactercharch = getCharFromString(str, index);System.out.println("Character from "+ str+" at index "+ index+" is "+ ch);}}chevron_rightfilter_noneOutput:Character from GeeksForGeeks at index 5 is F
Recommended Posts:
- Java program to count the occurrence of each character in a string using Hashmap
- Convert a String to Character array in Java
- Difference between String and Character array in Java
- Replace a character at a specific index in a String in Java
- Count occurrence of a given character in a string using Stream API in Java
- Java program to count the occurrences of each character
- Java Program to convert Character Array to IntStream
- Program to Convert Set of Integer to Set of String in Java
- Java program to expand a String if range is given?
- Java program to check whether a string is a Palindrome
- Program to convert set of String to set of Integer in Java
- Program to convert IntStream to String in Java
- Program to convert String to IntStream in Java
- Java.lang.Character.Subset Class in Java
- Java.lang.Character.UnicodeBlock Class in Java
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.



