Determine whether the java string contains a certain string

Java's String type provides the indexOf method, we can use it to determine whether a string of strings contains another string of strings, indexOf returns the subscript of the string, starting from 0, if there is no such string, return Is -1.

Look at the code:

public class StringTest {
    public static void main(String[] args) {
        String abc = "abc";
        System.out.println(abc.indexOf("a"));//0 存在 下标为0
        System.out.println(abc.indexOf("bc"));// 1 存在 下标为1
        System.out.println(abc.indexOf("ac"));// -1 不存在
        System.out.println(abc.indexOf("d"));//-1 不存在
    }
}

 

Guess you like

Origin blog.csdn.net/qq_36138652/article/details/107929491