Last Updated : 29 Jul, 2024
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: eInput: str = "GeeksForGeeks", index = 5
Output: F
Below are various ways to do so:
// 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);
}
}
Character from GeeksForGeeks at index 5 is F
// 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);
}
}
Character from GeeksForGeeks at index 5 is F
// 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);
}
}
Character from GeeksForGeeks at index 5 is F
// 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);
}
}
Character from GeeksForGeeks at index 5 is F
// 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);
}
}
Character from GeeksForGeeks at index 5 is F
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4