字符串的查找

Posted 竹之轩

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字符串的查找相关的知识,希望对你有一定的参考价值。

最简单的就是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

以上是关于字符串的查找的主要内容,如果未能解决你的问题,请参考以下文章