📌  相关文章
📜  Java程序从字符串中获取一个字符

📅  最后修改于: 2022-05-13 01:55:49.722000             🧑  作者: Mango

Java程序从字符串中获取一个字符

给定一个字符串 str,任务是从该字符串的特定索引处获取特定字符。

例子:

Input: str = "Geeks", index = 2
Output: e

Input: str = "GeeksForGeeks", index = 5
Output: F

以下是执行此操作的各种方法:

  • 使用 String.charAt() 方法
    1. 获取字符串和索引
    2. 使用 String.charAt(index) 方法获取特定字符。
    3. 返回特定字符。

    下面是上述方法的实现:

    // 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() 方法
    1. 获取字符串和索引
    2. 使用 String.toCharArray() 方法将字符串转换为字符数组。
    3. 获取字符数组特定索引处的特定字符。
    4. 返回特定字符。

    下面是上述方法的实现:

    // 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 流
    1. 获取字符串和索引
    2. 使用 String.chars() 方法将 String 转换为 IntStream。
    3. 将 IntStream 转换为 Stream使用 IntStream.mapToObj() 方法。
    4. 转换流使用 toArray() 方法进入字符[]。
    5. 从此字符数组中获取特定索引处的元素。
    6. 返回特定字符。

    下面是上述方法的实现:

    // 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() 方法
    1. 获取字符串和索引
    2. 使用 String.codePointAt() 方法获取特定索引处的特定字符ASCII 值。
    3. 使用类型转换将此 ASCII 值转换为字符。
    4. 返回特定字符。

    下面是上述方法的实现:

    // 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. 获取字符串和索引
    2. 创建一个大小为 1 的空字符数组
    3. 使用 String.getChars() 方法将特定索引处的元素从 String 复制到 char[] 中。
    4. 获取字符数组索引 0 处的特定字符。
    5. 返回特定字符。

    下面是上述方法的实现:

    // 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