Home > Java > javaTutorial > How to use the contains() method of the String class for string matching

How to use the contains() method of the String class for string matching

WBOY
Release: 2023-07-24 18:18:16
Original
2622 people have browsed it

How to use the contains() method of the String class for string matching

In Java programming, string processing is a very common operation. String matching is one of the common operations used to determine whether a string contains another string. Java's String class provides the contains() method to facilitate string matching. This article will introduce in detail how to use the contains() method of the String class for string matching and give code examples.

String class is a common class used to process strings in Java. This class provides various methods to manipulate and process strings. Among them, the contains() method is used to determine whether a string contains another string. The definition of this method is:

public boolean contains(CharSequence sequence)

This method receives a parameter of type CharSequence, which is a string. Returns a Boolean value, true if the called string contains the parameter string, false otherwise.

Below we illustrate the use of this method through several examples.

Example 1:

String str = "Hello World";
String searchStr = "Hello";

boolean result = str.contains(searchStr);

System.out.println(result); // 输出:true
Copy after login

In the above example, we defined a string str with the value "Hello World". Then a searchStr is defined with the value "Hello". Then call the contains() method of the str object, passing in searchStr as a parameter. Finally, print out the returned result, which is true. Because str contains searchStr, true is returned.

Example 2:

String str = "Hello World";
String searchStr = "hello";

boolean result = str.contains(searchStr);

System.out.println(result); // 输出:false
Copy after login

In the above example, we change the value of searchStr to "hello". Although the two strings are very similar, since Java strings are case-sensitive, the return result is false. Because str does not contain searchStr.

In addition to determining whether a string contains another string, the contains() method can also be used to determine whether a string contains a certain character:

Example 3:

String str = "Hello World";

boolean result = str.contains("W");

System.out.println(result); // 输出:true
Copy after login

In the above example, we call the contains() method by passing in a character as a parameter. The return result is true because str contains the character "W".

It should be noted that the contains() method is case-sensitive. If you need to ignore case for matching, you can use the equalsIgnoreCase() method.

Example 4:

String str = "Hello World";
String searchStr = "hello";

boolean result = str.toLowerCase().contains(searchStr.toLowerCase());

System.out.println(result); // 输出:true
Copy after login

In the above example, we call the contains() method by converting two strings to lowercase, thereby ignoring case. The return result is true because str contains searchStr.

Use the contains() method of the String class for string matching, which can easily determine whether a string contains another string. Depending on the situation, subsequent operations can be performed based on the returned Boolean value. Through the introduction and examples of this article, I believe readers have mastered how to use the contains() method for string matching.

Summary:

This article introduces how to use the contains() method of the String class in Java for string matching. By calling this method, you can easily determine whether a string contains another string or character. Note that this method is case-sensitive. If you need to ignore case for matching, you can do so by converting to lowercase. Such string matching operations are very commonly used in actual programming. I hope this article will be helpful to readers.

The above is the detailed content of How to use the contains() method of the String class for string matching. For more information, please follow other related articles on the PHP Chinese website!

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