首页 > 其他 > 详细

字符串的查找

时间:2017-09-02 15:20:23      阅读:227      评论:0      收藏:0      [点我收藏+]

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

字符串的查找

原文:http://www.cnblogs.com/123talents/p/7466649.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!