Check if a String starts with any of the given prefixes in Java

Given a String and an array of prefixes. The task is to check whether the given String starts with any of the given prefixes or not.

Example:

Input: String = “GeeksforGeeks”, Prefixes = {“Geeks”, “for”, “Gfor”}
Output: true

Input: String = “GeeksforGeeks”, Prefixes = {“Freaks”, “for”, “Freak”}
Output: false



Below are the following approaches that can be used to complete the given task:

  1. Naive Approach: This method involves the checking the String for each of the prefix array element explicitly.

    Algorithm:

    1. Get the string and the prefixes to be matched with.
    2. Using loop, iterate through the prefixes and check whether the string starts with the respective prefix. This is done with the help of String.startsWith() method.
    3. If any prefix is matched, then return true else return false.

    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    class PrefixSearch {
        public static void main(String[] args)
        {
            // Array of prefixes
            String[] arr = { "Geeks", "for", "Gfor" };
      
            // Given string
            String str = "GeeksforGeeks";
      
            boolean result = false;
      
            // Check for each prefix element
            for (int i = 0; i < 3; i++) {
                if (str.startsWith(arr[i])) {
                    result = true;
                    break;
                }
            }
      
            if (result)
                System.out.println("Given String "
                                   + "starts with one of the prefixes.");
            else
                System.out.println("Given String do not "
                                   + "starts with one of the prefixes.");
        }
    }

    chevron_right

    
    

    Output:

    Given String starts with one of the prefixes.
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    class PrefixSearch {
        public static void main(String[] args)
        {
            // Array of prefixes
            String[] arr = { "Freaks", "for", "Freak" };
      
            // Given string
            String str = "GeeksforGeeks";
      
            boolean result = false;
      
            // Check for each prefix element
            for (int i = 0; i < 3; i++) {
                if (str.startsWith(arr[i])) {
                    result = true;
                    break;
                }
            }
      
            if (result)
                System.out.println("Given String "
                                   + "starts with one of the prefixes.");
            else
                System.out.println("Given String do not "
                                   + "starts with one of the prefixes.");
        }
    }

    chevron_right

    
    

    Output:

    Given String do not starts with one of the prefixes.
    
  2. Using Regular expression:

    Algorithm:

    1. Get the string and the prefixes to be matched with.
    2. Form a Regular Expression to check if the string starts with any of the prefix. This can be done using String.matches() method.
    3. If any prefix is matched, then return true else return false.

    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    class PrefixSearch {
        public static void main(String[] args)
        {
            // Array of prefixes
            String[] arr = { "Geeks", "for", "Gfor" };
      
            // Given String
            String str = "GeeksforGeeks";
      
            // Check for prefixes using Regex
            if (str.matches("(" + arr[0] + "|"
                            + arr[1] + "|"
                            + arr[2] + ").*"))
                System.out.println("Given String "
                                   + "starts with one of the prefixes.");
            else
                System.out.println("Given String do not "
                                   + "starts with one of the prefixes.");
        }
    }

    chevron_right

    
    

    Output:

    Given String starts with one of the prefixes.
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    class PrefixSearch {
        public static void main(String[] args)
        {
            // Array of prefixes
            String[] arr = { "Freaks", "for", "Freak" };
      
            // Given String
            String str = "GeeksforGeeks";
      
            // Check for prefixes using Regex
            if (str.matches("(" + arr[0] + "|"
                            + arr[1] + "|"
                            + arr[2] + ").*"))
                System.out.println("Given String "
                                   + "starts with one of the prefixes.");
            else
                System.out.println("Given String do not "
                                   + "starts with one of the prefixes.");
        }
    }

    chevron_right

    
    

    Output:

    Given String do not starts with one of the prefixes.
    
  3. Using Java 8 Streams API:

    Algorithm:

    1. Get the string and the prefixes to be matched with.
    2. Convert the Prefixes into Stream using Stream.of()
    3. Check if any prefix matches using Predicate str::startsWith. This is done using Stream.anyMatch() method.
    4. If any prefix is matched, then return true else return false.

    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import java.util.stream.Stream;
      
    class PrefixSearch {
        public static void main(String[] args)
        {
            // Array of prefixes
            String[] arr = { "Geeks", "for", "Gfor" };
      
            // Given String
            String str = "GeeksforGeeks";
      
            // Convert the Prefixes into Stream using Stream.of()
            // and check if any prefix matches using Predicate
            // str::startsWith
            if (Stream.of(arr)
                    .anyMatch(str::startsWith))
                System.out.println("Given String "
                                   + "starts with one of the prefixes.");
            else
                System.out.println("Given String do not "
                                   + "starts with one of the prefixes.");
        }
    }

    chevron_right

    
    

    Output:

    Given String starts with one of the prefixes.
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import java.util.stream.Stream;
      
    class PrefixSearch {
        public static void main(String[] args)
        {
            // Array of prefixes
            String[] arr = { "Freaks", "for", "Freak" };
      
            // Given String
            String str = "GeeksforGeeks";
      
            // Convert the Prefixes into Stream using Stream.of()
            // and check if any prefix matches using Predicate
            // str::startsWith
            if (Stream.of(arr)
                    .anyMatch(str::startsWith))
                System.out.println("Given String "
                                   + "starts with one of the prefixes.");
            else
                System.out.println("Given String do not "
                                   + "starts with one of the prefixes.");
        }
    }

    chevron_right

    
    

    Output:

    Given String do not starts with one of the prefixes.
    


My Personal Notes arrow_drop_up

Technical Content Engineer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.