Analyzing the java contains a string or a character string

A, the contains the method
returns true java.lang.String.contains () method, if and only if the specified string comprising a sequence of char values

This method returns true if this string contains, otherwise false.

public static void main(String[] args) {    
        String str = "abc";    
        boolean status = str.contains("a");    
        if(status){    
            System.out.println("包含");    
        }else{    
            System.out.println("不包含");    
        }    
    }   

Two, IndexOf method
java.lang.String.indexOf () purpose is to find the position of a word in a string, but it may be determined whether a string contains a character.

public static void main(String[] args) {    
    String str1 = "abcdefg";    
    int result1 = str1.indexOf("e");    
    if(result1 != -1){    
        System.out.println("字符串str中包含子串“e”,它在字符串中的位置是:"+result1);    
    }else{    
        System.out.println("字符串str中不包含子串“e”"+result1);    
    }    
}  
Published 156 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_42590334/article/details/103918509