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

How to tell if a string contains another string in Java

PHPz
PHPzforward
2023-04-25 10:16:062006browse

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));     } }

The output result is as follows:

true

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

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

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete