Java程序从字符串中获取一个字符
给定一个字符串 str,任务是从该字符串的特定索引处获取特定字符。
例子:
Input: str = "Geeks", index = 2
Output: e
Input: str = "GeeksForGeeks", index = 5
Output: F
以下是执行此操作的各种方法:
- 使用 String.charAt() 方法:
- 获取字符串和索引
- 使用 String.charAt(index) 方法获取特定字符。
- 返回特定字符。
下面是上述方法的实现:
// 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); } }
输出:Character from GeeksForGeeks at index 5 is F
- 使用 String.toCharArray() 方法:
- 获取字符串和索引
- 使用 String.toCharArray() 方法将字符串转换为字符数组。
- 获取字符数组特定索引处的特定字符。
- 返回特定字符。
下面是上述方法的实现:
// 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 8 流:
- 获取字符串和索引
- 使用 String.chars() 方法将 String 转换为 IntStream。
- 将 IntStream 转换为 Stream
使用 IntStream.mapToObj() 方法。 - 转换流
使用 toArray() 方法进入字符[]。 - 从此字符数组中获取特定索引处的元素。
- 返回特定字符。
- 转换流
下面是上述方法的实现:
// 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
.mapToObj(ch -> (char)ch) // Convert Stream 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
- 使用 String.codePointAt() 方法:
- 获取字符串和索引
- 使用 String.codePointAt() 方法获取特定索引处的特定字符ASCII 值。
- 使用类型转换将此 ASCII 值转换为字符。
- 返回特定字符。
下面是上述方法的实现:
// 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
- 使用 String.getChars() 方法:
- 获取字符串和索引
- 创建一个大小为 1 的空字符数组
- 使用 String.getChars() 方法将特定索引处的元素从 String 复制到 char[] 中。
- 获取字符数组索引 0 处的特定字符。
- 返回特定字符。
下面是上述方法的实现:
// 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