String a = "HelloWorld";
String b = "oWor";
如何符合判断a中是否包含b?

解决方案 »

  1.   

    int indexOf(String str) 
              Returns the index within this string of the first occurrence of the specified substring.
      

  2.   

    楼上的不行呀
    a.indexOf(b)>=0a.indexOf(b)!=-1
      

  3.   

    public class Text4 {    public Text4() {
        }
        
        public static void main (String[] args) {
         String a = "HelloWorld";
    String b = "oWor";   System.out.println(a.indexOf(b));
        }
        
    }输出 4  如果不包含输入为 -1public int indexOf(String str)
    Returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that: 
     this.startsWith(str, k)
     
    is true. Parameters:
    str - any string. 
    Returns:
    if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.
      

  4.   

    可以用正则表达式:
    public class RegexTest {
        public static void main(String args[]) {        Pattern pattern = Pattern.compile("oWor");
            Matcher matcher = pattern.matcher("HelloWorld");
            if(matcher.find()) {
                System.out.println("Bingo!");
            }else{
                System.out.println("No substring here!");
            }    }
    }