Home > Java > javaTutorial > body text

Use Java's String.endsWith() function to determine whether a string ends with a specified suffix

PHPz
Release: 2023-07-26 11:56:15
Original
2285 people have browsed it

Use Java's String.endsWith() function to determine whether a string ends with a specified suffix

In Java programming, we often need to determine whether a string ends with a specified suffix. In this case, you can Use the endsWith() function provided by the String class to determine. Through this function, we can effectively determine whether a string ends with the specified suffix.

String class is a class used to represent strings in Java. It provides many methods for operating strings. Among them, the endsWith() function is used to determine whether a string ends with the specified suffix. Its definition is as follows:

public boolean endsWith(String suffix)

The function of this function is very simple. It accepts a String type parameter suffix, which is used to specify the suffix string to be judged. If the string calling this function ends with suffix, that is, if the condition is met, true is returned; otherwise, false is returned.

The following is an example that demonstrates how to use the endsWith() function to determine whether a string ends with a specified suffix:

public class EndsWithExample {
    public static void main(String[] args) {
        String str1 = "Hello World";
        String str2 = "Java Programming";

        System.out.println("str1 ends with World: " + str1.endsWith("World")); // true
        System.out.println("str1 ends with Hello: " + str1.endsWith("Hello")); // false
        System.out.println("str2 ends with Programming: " + str2.endsWith("Programming")); // true
        System.out.println("str2 ends with Java: " + str2.endsWith("Java")); // false
    }
}
Copy after login

In the above example, we defined two string variables str1 and str2, "Hello World" and "Java Programming" respectively. Then, we use the endsWith() function to determine whether the two strings end with the specified suffix.

Run the above code, the output result is as follows:

str1 ends with World: true
str1 ends with Hello: false
str2 ends with Programming: true
str2 ends with Java: false
Copy after login

It can be seen from the result that str1 ends with "World", so calling str1.endsWith("World") returns true; instead of "Hello" ends, so calling str1.endsWith("Hello") returns false. Similarly, str2 ends with "Programming", so calling str2.endsWith("Programming") returns true; it does not end with "Java", so calling str2.endsWith("Java") returns false.

To summarize, using the endsWith() function of Java's String class, you can easily determine whether a string ends with a specified suffix. This function is very useful in actual programming work, especially in scenarios such as processing file names and URLs. It can help us process strings more efficiently.

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template