String的indexOf()方法不就是做这个的吗?

解决方案 »

  1.   

    String s = "1234567890";
    int i = 0;
    i = s.indexOf(“789”);
    则i得到值为6。
      

  2.   

    如楼上 namowen(寒号不已)所写就可以,有什么问题吗?
      

  3.   

    如,我要判断一个String1中有无另一个string2,若有就把String1=" ";如何写?
      

  4.   

    String String1 = ...;
    String String2 = ...;
    int i = 0;
    i = String1.indexOf(String2);if( i != -1 )
       String1=" ";
      

  5.   

    indexOfpublic 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.Throws:
    NullPointerException - if str is null.
      

  6.   

    String String1 = ...;
    String String2 = ...;
    int i = 0;
    i = String1.indexOf(String2);if( i != -1 )
       String1=" ";
    xtky_limi(痛在心中笑在脸上)    is true
      

  7.   

    谢谢,但我运行之后,有下面的出错信息:
    java.lang.NullPointerException at delprog.delFile2.main(delFile2.java:29)Exception in thread "main"