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:
class GFG {
public static char
getCharFromString(String str, int index)
{
return str.charAt(index);
}
public static void main(String[] args)
{
String str = "GeeksForGeeks" ;
int index = 5 ;
char ch = getCharFromString(str, index);
System.out.println( "Character from " + str
+ " at index " + index
+ " is " + ch);
}
}
|
Output:
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:
class GFG {
public static char
getCharFromString(String str, int index)
{
return str.toCharArray()[index];
}
public static void main(String[] args)
{
String str = "GeeksForGeeks" ;
int index = 5 ;
char ch = getCharFromString(str, index);
System.out.println( "Character from " + str
+ " at index " + index
+ " is " + ch);
}
}
|
Output:
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:
class GFG {
public static char
getCharFromString(String str, int index)
{
return str
.chars()
.mapToObj(ch -> ( char )ch)
.toArray(Character[] :: new )[index];
}
public static void main(String[] args)
{
String str = "GeeksForGeeks" ;
int index = 5 ;
char ch = getCharFromString(str, index);
System.out.println( "Character from " + str
+ " at index " + index
+ " is " + ch);
}
}
|
Output:
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:
class GFG {
public static char
getCharFromString(String str, int index)
{
return ( char )str.codePointAt(index);
}
public static void main(String[] args)
{
String str = "GeeksForGeeks" ;
int index = 5 ;
char ch = getCharFromString(str, index);
System.out.println( "Character from " + str
+ " at index " + index
+ " is " + ch);
}
}
|
Output:
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:
class GFG {
public static char
getCharFromString(String str, int index)
{
char [] singleCharArray = new char [ 1 ];
str.getChars(index, index + 1 , singleCharArray, 0 );
return singleCharArray[ 0 ];
}
public static void main(String[] args)
{
String str = "GeeksForGeeks" ;
int index = 5 ;
char ch = getCharFromString(str, index);
System.out.println( "Character from " + str
+ " at index " + index
+ " is " + ch);
}
}
|
Output:
Character from GeeksForGeeks at index 5 is F
Last Updated :
19 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...