字符串的查找与替换

最简单的就是contains()方法

String str = "helloworld";

System.out.println(str.contains("world"));   // true    jdk 1.5 后才有的

System.out.println(str.indexOf("world"));   //  5  w 开始的索引   jdk 1.5 前采用indexOf() 方法    

System.out.println(str.indexOf("java"));    // -1 没有查到

if(str.indexOf("world")!=-1) 

{

  System.out.println("可以查找到指定的内容!");

}

建议使用contains()方法

 

字符串的替换

String str = "helloworld";

System.out.println(str.replaceAll("l","_"));     //  he__oworld

System.out.println(str.replaceFirst("l","_"));  //  he_loworld

posted @ 2017-09-02 14:26  竹之轩  阅读(1295)  评论(0编辑  收藏  举报