Monday, November 18, 2024
Google search engine
HomeData Modelling & AIJava Program to get a character from a String

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:
    1. Get the string and the index
    2. Get the specific character using String.charAt(index) method.
    3. 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:
    1. Get the string and the index
    2. Convert the String into Character array using String.toCharArray() method.
    3. Get the specific character at the specific index of the character array.
    4. 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:
    1. Get the string and the index
    2. Convert String into IntStream using String.chars() method.
    3. Convert IntStream into Stream using IntStream.mapToObj() method.
    4. Convert Stream into Character[] using toArray() method.
    5. Get the element at the specific index from this character array.
    6. 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:
    1. Get the string and the index
    2. Get the specific character ASCII value at the specific index using String.codePointAt() method.
    3. Convert this ASCII value into character using type casting.
    4. 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:
    1. Get the string and the index
    2. Create an empty char array of size 1
    3. Copy the element at specific index from String into the char[] using String.getChars() method.
    4. Get the specific character at the index 0 of the character array.
    5. 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
    

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments