每天学点Java知识 ------> String类常用方法(字符串查找)

上一篇 / 下一篇  2014-07-28 11:24:18 / 个人分类:Java学习

1.public boolean contains(String s)
查找指定的子字符串是否存在
2.public int indexOf(String str)
从头查找指定字符串的位置,找不到返回-1
3.public int indexOf(String str, int fromIndex)
由指定位置向后查找字符串的位置,找不到返回-1
4.public int lastIndexOf(String str)
由后向前查找字符串的位置,找不到返回-1
5.public int lastIndexOf(String str, int fromIndex)
从指定位置由后向前查找
6.public boolean startsWith(String prefix)
判断是否以指定的字符串开头
7.public boolean startsWith(String prefix, int toffset)
从指定位置判断是否以指定字符串开头
8.public boolean endsWith(String suffix)
判断是否以指定的字符串结尾

public class TestJava{
 public static void main(String args[]){
  String str = "Hello World!";
  System.out.println(str.contains("Hello"));
  System.out.println(str.contains("hi"));
 }
}
程序运行结果:
true
false
 
 
public class TestJava{
 public static void main(String args[]){
  String str = "Hello World!";
  System.out.println(str.startsWith("H"));
  System.out.println(str.startsWith("l", 2));
  System.out.println(str.endsWith("!"));
 }
}
程序运行结果:
true
true
true

TAG:

 

评分:0

我来说两句

日历

« 2024-04-25  
 123456
78910111213
14151617181920
21222324252627
282930    

我的存档

数据统计

  • 访问量: 30802
  • 日志数: 41
  • 建立时间: 2014-07-17
  • 更新时间: 2014-07-30

RSS订阅

Open Toolbar