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

11 Dec, 2018
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:
Naive Approach: This method involves the checking the String for each of the prefix array element explicitly.Algorithm:Get the string and the prefixes to be matched with.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.If any prefix is matched, then return true else return false.Program 1: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.");    }}Output:Given String starts with one of the prefixes.
Program 2: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.");    }}Output:Given String do not starts with one of the prefixes.
Using Regular expression:Algorithm:Get the string and the prefixes to be matched with.Form a Regular Expression to check if the string starts with any of the prefix. This can be done using String.matches() method.If any prefix is matched, then return true else return false.Program 1: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.");    }}Output:Given String starts with one of the prefixes.
Program 2: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.");    }}Output:Given String do not starts with one of the prefixes.
Using Java 8 Streams API:Algorithm:Get the string and the prefixes to be matched with.Convert the Prefixes into Stream using Stream.of()Check if any prefix matches using Predicate str::startsWith. This is done using Stream.anyMatch() method.If any prefix is matched, then return true else return false.Program 1: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.");    }}Output:Given String starts with one of the prefixes.
Program 2: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.");    }}Output:Given String do not starts with one of the prefixes.

Naive Approach: This method involves the checking the String for each of the prefix array element explicitly.Algorithm:Get the string and the prefixes to be matched with.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.If any prefix is matched, then return true else return false.Program 1: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.");    }}Output:Given String starts with one of the prefixes.
Program 2: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.");    }}Output:Given String do not starts with one of the prefixes.

Algorithm:
Get the string and the prefixes to be matched with.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.If any prefix is matched, then return true else return false.
Get the string and the prefixes to be matched with.
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.
If any prefix is matched, then return true else return false.
Program 1:
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.");    }}
Output:
Given String starts with one of the prefixes.

Program 2:
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.");    }}
Output:
Given String do not starts with one of the prefixes.

Using Regular expression:Algorithm:Get the string and the prefixes to be matched with.Form a Regular Expression to check if the string starts with any of the prefix. This can be done using String.matches() method.If any prefix is matched, then return true else return false.Program 1: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.");    }}Output:Given String starts with one of the prefixes.
Program 2: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.");    }}Output:Given String do not starts with one of the prefixes.

Algorithm:
Get the string and the prefixes to be matched with.Form a Regular Expression to check if the string starts with any of the prefix. This can be done using String.matches() method.If any prefix is matched, then return true else return false.
Get the string and the prefixes to be matched with.
Form a Regular Expression to check if the string starts with any of the prefix. This can be done using String.matches() method.
If any prefix is matched, then return true else return false.
Program 1:
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.");    }}
Output:
Given String starts with one of the prefixes.

Program 2:
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.");    }}
Output:
Given String do not starts with one of the prefixes.

Using Java 8 Streams API:Algorithm:Get the string and the prefixes to be matched with.Convert the Prefixes into Stream using Stream.of()Check if any prefix matches using Predicate str::startsWith. This is done using Stream.anyMatch() method.If any prefix is matched, then return true else return false.Program 1: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.");    }}Output:Given String starts with one of the prefixes.
Program 2: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.");    }}Output:Given String do not starts with one of the prefixes.

Algorithm:
Get the string and the prefixes to be matched with.Convert the Prefixes into Stream using Stream.of()Check if any prefix matches using Predicate str::startsWith. This is done using Stream.anyMatch() method.If any prefix is matched, then return true else return false.
Get the string and the prefixes to be matched with.
Convert the Prefixes into Stream using Stream.of()
Check if any prefix matches using Predicate str::startsWith. This is done using Stream.anyMatch() method.
If any prefix is matched, then return true else return false.
Program 1:
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.");    }}
Output:
Given String starts with one of the prefixes.

Program 2:
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.");    }}
Output:
Given String do not starts with one of the prefixes.

Java-String-Programs
Java-Strings
prefix
Java
Java Programs
Java-Strings
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Initialize an ArrayList in Java
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Interfaces in Java
How to iterate any Map in Java
Initializing a List in Java
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class