Home  >  Article  >  Java  >  Use Java's String.startsWith() function to determine whether a string starts with a specified prefix

Use Java's String.startsWith() function to determine whether a string starts with a specified prefix

WBOY
WBOYOriginal
2023-07-25 14:27:251878browse

Use Java's String.startsWith() function to determine whether a string starts with the specified prefix

In Java programming, we often need to process and judge strings. One common operation is to determine whether a string begins with a specified prefix. Java provides the startsWith() function of the String class, which can easily implement this function.

String’s startsWith() function

The String class is one of the most commonly used classes in Java. It provides many commonly used string processing and judgment methods, including the startsWith() function. By calling this function, we can determine whether a string begins with the specified prefix.

The function is defined as follows:
public boolean startsWith(String prefix)

The parameter prefix is ​​a string indicating the prefix to be judged. The return value of the function is a Boolean value. If the current string starts with prefix, it returns true, otherwise it returns false.

Code example

The following is a sample code using the startsWith() function:

public class StringStartsWithExample {
    public static void main(String[] args) {
        String str1 = "Hello, world!";
        String str2 = "Hello";
        String str3 = "World";

        // 判断字符串是否以指定前缀开头
        boolean startsWith1 = str1.startsWith("Hello");
        boolean startsWith2 = str2.startsWith("Hello");
        boolean startsWith3 = str3.startsWith("Hello");

        // 输出结果
        System.out.println("str1 starts with "Hello": " + startsWith1);
        System.out.println("str2 starts with "Hello": " + startsWith2);
        System.out.println("str3 starts with "Hello": " + startsWith3);
    }
}

In the above example, we define three strings str1, str2 and str3, respectively represent different strings.

Then, we use the startsWith() function to determine whether each string is prefixed with "Hello".

Finally, print the result by calling the System.out.println() function.

The result output is as follows:

str1 starts with "Hello": true
str2 starts with "Hello": true
str3 starts with "Hello": false

As can be seen from the result, both str1 and str2 are prefixed with "Hello", so the return value of the startsWith() function is true. And str3 is not prefixed with "Hello", so the return value is false.

Summary

Using Java's String.startsWith() function can easily determine whether a string starts with a specified prefix. By calling this function, we can simply match string prefixes and get the corresponding Boolean result. Whether in string processing or judgment logic, the startsWith() function is a practical tool function.

The above is the detailed content of Use Java's String.startsWith() function to determine whether a string starts with a specified prefix. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn