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 index
Â
Âclass
GFG {
Â
ÂÂ Â Â Â
// Function to get the specific character
   Â
public
static
char
   Â
getCharFromString(String str,
int
index)
   Â
{
       Â
return
str.charAt(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);
   Â
}
}
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:
// 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);
   Â
}
}
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:
// 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);
   Â
}
}
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:
// 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);
   Â
}
}
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:
// 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);
   Â
}
}
Output:Character from GeeksForGeeks at index 5 is F
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!