每天学点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: