Java 检查一个字符串是否以任何给定的前缀开始

Java 检查一个字符串是否以任何给定的前缀开始

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

例子。

输入 :String = “GeeksforGeeks”, 前缀 = {“Geeks”, “for”, “Gfor” }

输出 :true

输入 :字符串 = “GeeksforGeeks”, 前缀 = {“Freaks”, “for”, “Freak” }

输出 : false

建议:请先在 {IDE}上尝试你的方法,然后再继续解决

以下是可以用来完成给定任务的方法。

  1. Naive 方法 : 这个方法涉及到明确检查每个前缀数组元素的字符串。

算法。

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

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

输出:

Given String starts with one of the prefixes.

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

输出:

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

算法。

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

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

输出:

Given String starts with one of the prefixes.

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

输出:

Given String do not starts with one of the prefixes.
  1. 使用 Java 8 Streams API :

算法。

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

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

输出:

Given String starts with one of the prefixes.

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

输出:

Given String do not starts with one of the prefixes.

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程