Home > Java > javaTutorial > How to tell if a string contains another string in Java

How to tell if a string contains another string in Java

PHPz
Release: 2023-04-25 10:16:06
forward
2068 people have browsed it

How to judge that a string contains another string

This question is a bit simple, right? The previous question also used Stream stream, so this question will give you points directly ?Don’t doubt yourself, just use the contains() method of the string class.

public class StringContainsSubstring {     public static void main(String[] args) {         String s1 = "沉默王二";         String s2 = "沉默";          System.out.println(s1.contains(s2));     } }
Copy after login

The output result is as follows:

true
Copy after login

contains() method actually calls the indexOf() method:

public boolean contains(CharSequence s) {     return indexOf(s.toString()) >= 0; }
Copy after login

The above is the detailed content of How to tell if a string contains another string in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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