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:
- 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:
classPrefixSearch {publicstaticvoidmain(String[] args){// Array of prefixesString[] arr = {"Geeks","for","Gfor"};// Given stringString str ="GeeksforGeeks";booleanresult =false;// Check for each prefix elementfor(inti =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.");elseSystem.out.println("Given String do not "+"starts with one of the prefixes.");}}chevron_rightfilter_noneOutput:
Given String starts with one of the prefixes.
Program 2:
classPrefixSearch {publicstaticvoidmain(String[] args){// Array of prefixesString[] arr = {"Freaks","for","Freak"};// Given stringString str ="GeeksforGeeks";booleanresult =false;// Check for each prefix elementfor(inti =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.");elseSystem.out.println("Given String do not "+"starts with one of the prefixes.");}}chevron_rightfilter_noneOutput:
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:
classPrefixSearch {publicstaticvoidmain(String[] args){// Array of prefixesString[] arr = {"Geeks","for","Gfor"};// Given StringString str ="GeeksforGeeks";// Check for prefixes using Regexif(str.matches("("+ arr[0] +"|"+ arr[1] +"|"+ arr[2] +").*"))System.out.println("Given String "+"starts with one of the prefixes.");elseSystem.out.println("Given String do not "+"starts with one of the prefixes.");}}chevron_rightfilter_noneOutput:
Given String starts with one of the prefixes.
Program 2:
classPrefixSearch {publicstaticvoidmain(String[] args){// Array of prefixesString[] arr = {"Freaks","for","Freak"};// Given StringString str ="GeeksforGeeks";// Check for prefixes using Regexif(str.matches("("+ arr[0] +"|"+ arr[1] +"|"+ arr[2] +").*"))System.out.println("Given String "+"starts with one of the prefixes.");elseSystem.out.println("Given String do not "+"starts with one of the prefixes.");}}chevron_rightfilter_noneOutput:
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:
importjava.util.stream.Stream;classPrefixSearch {publicstaticvoidmain(String[] args){// Array of prefixesString[] arr = {"Geeks","for","Gfor"};// Given StringString str ="GeeksforGeeks";// Convert the Prefixes into Stream using Stream.of()// and check if any prefix matches using Predicate// str::startsWithif(Stream.of(arr).anyMatch(str::startsWith))System.out.println("Given String "+"starts with one of the prefixes.");elseSystem.out.println("Given String do not "+"starts with one of the prefixes.");}}chevron_rightfilter_noneOutput:
Given String starts with one of the prefixes.
Program 2:
importjava.util.stream.Stream;classPrefixSearch {publicstaticvoidmain(String[] args){// Array of prefixesString[] arr = {"Freaks","for","Freak"};// Given StringString str ="GeeksforGeeks";// Convert the Prefixes into Stream using Stream.of()// and check if any prefix matches using Predicate// str::startsWithif(Stream.of(arr).anyMatch(str::startsWith))System.out.println("Given String "+"starts with one of the prefixes.");elseSystem.out.println("Given String do not "+"starts with one of the prefixes.");}}chevron_rightfilter_noneOutput:
Given String do not starts with one of the prefixes.
Recommended Posts:
- Check for string rotation in Java
- Check if a string contains only alphabets in Java using Regex
- Java program to check whether a string is a Palindrome
- Check if a string contains only alphabets in Java using Lambda expression
- Check if a string contains only alphabets in Java using ASCII values
- Check if a given string is a valid number (Integer or Floating Point) in Java
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Count all prefixes of the given binary array which are divisible by x
- Convert a List of String to a comma separated String in Java
- Convert a Set of String to a comma separated String in Java
- Convert an ArrayList of String to a String array in Java
- Convert Set of String to Array of String in Java
- String Literal Vs String Object in Java
- Check if a binary string has two consecutive occurrences of one everywhere
- Insert a String into another String in Java
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.



