javaの解析は、文字列または文字列が含まれています

Aは、メソッド含ま
戻り真java.lang.String.containsを()メソッド、場合に限りchar値の配列を含む、指定された文字列

この文字列が含まれている場合、このメソッドはそうでない場合はfalse、trueを返します。

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

二つは、IndexOfメソッドメソッド
java.lang.String.indexOf()の目的は、文字列内の単語の位置を見つけることですが、文字列に文字が含まれているかどうかを判定することができます。

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);    
    }    
}  
公開された156元の記事 ウォンの賞賛8 ビュー20000 +

おすすめ

転載: blog.csdn.net/weixin_42590334/article/details/103918509