📌  相关文章
📜  检查字符串是否以Java中的任何给定前缀开头

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

检查字符串是否以Java中的任何给定前缀开头

给定一个字符串和一个前缀数组。任务是检查给定的字符串是否以任何给定的前缀开头。

例子:

以下是可用于完成给定任务的以下方法:

  1. 天真的方法:此方法涉及显式检查每个前缀数组元素的字符串。

    算法:

    1. 获取要匹配的字符串和前缀。
    2. 使用循环,遍历前缀并检查字符串是否以相应的前缀开头。这是在 String.startsWith() 方法的帮助下完成的。
    3. 如果匹配任何前缀,则返回 true,否则返回 false。

    方案一:

    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.");
        }
    }
    

    输出:

    Given String starts with one of the prefixes.
    

    方案二:

    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.");
        }
    }
    

    输出:

    Given String do not starts with one of the prefixes.
    
  2. 使用正则表达式

    算法:

    1. 获取要匹配的字符串和前缀。
    2. 形成一个正则表达式来检查字符串是否以任何前缀开头。这可以使用 String.matches() 方法来完成。
    3. 如果匹配任何前缀,则返回 true,否则返回 false。

    方案一:

    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.");
        }
    }
    

    输出:

    Given String starts with one of the prefixes.
    

    方案二:

    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.");
        }
    }
    

    输出:

    Given String do not starts with one of the prefixes.
    
  3. 使用Java 8 流 API

    算法:

    1. 获取要匹配的字符串和前缀。
    2. 将前缀转换为流使用 Stream.of()
    3. 使用 Predicate str::startsWith 检查是否有任何前缀匹配。这是使用 Stream.anyMatch() 方法完成的。
    4. 如果匹配任何前缀,则返回 true,否则返回 false。

    方案一:

    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.");
        }
    }
    

    输出:

    Given String starts with one of the prefixes.
    

    方案二:

    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.");
        }
    }
    

    输出:

    Given String do not starts with one of the prefixes.