java实例

查找字符串最后一次出现的位置

//strOrig.lastIndexOf(Stringname)

 

Runoob 字符串最后出现的位置: 19

public class SearchlastString {

public static void main(String[] args) {

String strOrig = "Hello world ,Hello Runoob";

int lastIndex = strOrig.lastIndexOf("Runoob");

  if(lastIndex == - 1)  

  {

    System.out.println("没有找到字符串 Runoob");

  }

    else

    {

      System.out.println("Runoob 字符串最后出现的位置: "+ lastIndex);

    }

  }

}

 

字符串替换

 java String 类的 replace (str1,str2)

public class StringReplaceEmp{

   public static void main(String args[]){

      String str="Hello World";

      System.out.println( str.replace( 'H','W' ) );

      System.out.println( str.replaceFirst("He", "Wa") );

  System.out.println( str.replaceAll("He", "Ha") );
   }

}

Wello World
Wallo World
Hallo World
posted @ 2021-10-14 19:41  Cuora  阅读(34)  评论(0)    收藏  举报