Java determines whether a string contains a string

You must first introduce dependencies: import java.lang.String

1. contains: the return value is Boolean

    String str = "abcdefg";  
    boolean check = str.contains("cde");  
    if(check){  
        //包含指定字符串
    }else{  
        //不包含指定字符串
    }  

2. indexOf: the return value is int

    String str = "abcdefg";  
    int check = str.indexOf("cde");  
    if(check != -1){  
       //包含指定字符串
    }else{  
        //不包含指定字符串
    } 

Guess you like

Origin blog.csdn.net/Mrlujiao_code/article/details/112993547