码迷,mamicode.com
首页 > 其他好文 > 详细

字符串的查找

时间:2017-09-02 15:47:56      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:内容   简单的   int   contains   nbsp   指定   使用   字符   replace   

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

字符串的查找

标签:内容   简单的   int   contains   nbsp   指定   使用   字符   replace   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!