Java Program to Get a Character from a String Last Updated : 29 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Given a String str, the task is to get a specific character from that String at a specific index. Examples:Input: str = "Geeks", index = 2Output: eInput: str = "GeeksForGeeks", index = 5Output: F Below are various ways to do so: Using String.charAt() method: Get the string and the indexGet the specific character using String.charAt() method.Return the specific character. Below is the implementation of the above approach: Java // Java program to get a specific character // from a given String at a specific index class GFG{ // Driver code public static void main(String[] args) { // Get the String String str = "GeeksForGeeks"; // Get the index int index = 5; // Get the specific character char ch = str.charAt(index); System.out.println("Character from " + str + " at index " + index + " is " + ch); } } OutputCharacter from GeeksForGeeks at index 5 is F Using String.toCharArray() method: Get the string and the indexConvert 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 // Java program to get a specific character // from a given String at a specific index class GFG { // Function to get the specific character public static char getCharFromString(String str, int index) { return str.toCharArray()[index]; } // Driver code public static void main(String[] args) { // Get the String String str = "GeeksForGeeks"; // Get the index int index = 5; // Get the specific character char ch = getCharFromString(str, index); System.out.println("Character from " + str + " at index " + index + " is " + ch); } } OutputCharacter from GeeksForGeeks at index 5 is F Using Java 8 Streams: Get the string and the indexConvert 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 // Java program to get a specific character // from a given String at a specific index class GFG { // Function to get the specific character public static char getCharFromString(String str, int index) { return str // 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 code public static void main(String[] args) { // Get the String String str = "GeeksForGeeks"; // Get the index int index = 5; // Get the specific character char ch = getCharFromString(str, index); System.out.println("Character from " + str + " at index " + index + " is " + ch); } } OutputCharacter from GeeksForGeeks at index 5 is F Using String.codePointAt() method: Get the string and the indexGet 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 // Java program to get a specific character // from a given String at a specific index class GFG { // Function to get the specific character public static char getCharFromString(String str, int index) { return (char)str.codePointAt(index); } // Driver code public static void main(String[] args) { // Get the String String str = "GeeksForGeeks"; // Get the index int index = 5; // Get the specific character char ch = getCharFromString(str, index); System.out.println("Character from " + str + " at index " + index + " is " + ch); } } OutputCharacter from GeeksForGeeks at index 5 is F Using String.getChars() method: Get the string and the indexCreate an empty char array of size 1Copy 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 // Java program to get a specific character // from a given String at a specific index class GFG { // Function to get the specific character public static char getCharFromString(String str, int index) { // Create a character array of size 1 char[] singleCharArray = new char[1]; // Get the specific character from the String // into the char[] at index 0 str.getChars(index, index + 1, singleCharArray, 0); // Return the specific character // present at index 0 in char[] return singleCharArray[0]; } // Driver code public static void main(String[] args) { // Get the String String str = "GeeksForGeeks"; // Get the index int index = 5; // Get the specific character char ch = getCharFromString(str, index); System.out.println("Character from " + str + " at index " + index + " is " + ch); } } OutputCharacter from GeeksForGeeks at index 5 is F Comment More infoAdvertise with us R RishabhPrabhu Follow Improve Article Tags : Java Java-Strings Practice Tags : JavaJava-Strings Explore BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial15+ min readJava Networking15+ min readJDBC Tutorial12 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions7 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like