1、indexOf()
indexOf() 方法有以下四种形式:
-
public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
-
public int indexOf(int ch, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
-
int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
-
int indexOf(String str, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。\
public int indexOf(int ch ) 或 public int indexOf(int ch, int fromIndex) 或 int indexOf(String str) 或 int indexOf(String str, int fromIndex)
参数
ch -- 字符,Unicode 编码。
fromIndex -- 开始搜索的索引位置,第一个字符是 0 ,第二个是 1 ,以此类推。
str -- 要搜索的子字符串
例子
字符
public class Main {
public static void main(String args[]) {
String string = "aaa456ac";
//查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.
System.out.println(string.indexOf("b")); // indexOf(String str); 返回结果:-1,"b"不存在
// 从第四个字符位置开始往后继续查找,包含当前位置
System.out.println(string.indexOf("a",3));//indexOf(String str, int fromIndex); 返回结果:6
//(与之前的差别:上面的参数是 String 类型,下面的参数是 int 类型)参考数据:a-97,b-98,c-99
// 从头开始查找是否存在指定的字符
System.out.println(string.indexOf(99));//indexOf(int ch);返回结果:7
System.out.println(string.indexOf('c'));//indexOf(int ch);返回结果:7
//从fromIndex查找ch,这个是字符型变量,不是字符串。字符a对应的数字就是97。
System.out.println(string.indexOf(97,3));//indexOf(int ch, int fromIndex); 返回结果:6
System.out.println(string.indexOf('a',3));//indexOf(int ch, int fromIndex); 返回结果:6
}
}
字符串
public class Test {
public static void main(String args[]) {
String Str = new String("菜鸟教程:www.runoob.com");
String SubStr1 = new String("runoob");
String SubStr2 = new String("com");
System.out.print("查找字符 o 第一次出现的位置 :" );
System.out.println(Str.indexOf( 'o' ));
System.out.print("从第14个位置查找字符 o 第一次出现的位置 :" );
System.out.println(Str.indexOf( 'o', 14 ));
System.out.print("子字符串 SubStr1 第一次出现的位置:" );
System.out.println( Str.indexOf( SubStr1 ));
System.out.print("从第十五个位置开始搜索子字符串 SubStr1 第一次出现的位置 :" );
System.out.println( Str.indexOf( SubStr1, 15 ));
System.out.print("子字符串 SubStr2 第一次出现的位置 :" );
System.out.println(Str.indexOf( SubStr2 ));
}
}
//结果
//查找字符 o 第一次出现的位置 :12
//从第14个位置查找字符 o 第一次出现的位置 :17
//子字符串 SubStr1 第一次出现的位置:9
//从第十五个位置开始搜索子字符串 SubStr1 第一次出现的位置 :-1
//子字符串 SubStr2 第一次出现的位置 :16
2、lastIndexOf()
lastIndexOf() 方法有以下四种形式:
-
public int lastIndexOf(int ch): 返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
-
public int lastIndexOf(int ch, int fromIndex): 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索,如果此字符串中没有这样的字符,则返回 -1。
-
public int lastIndexOf(String str): 返回指定子字符串在此字符串中最右边出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
-
public int lastIndexOf(String str, int fromIndex): 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索,如果此字符串中没有这样的字符,则返回 -1。
public int lastIndexOf(int ch) 或 public int lastIndexOf(int ch, int fromIndex) 或 public int lastIndexOf(String str) 或 public int lastIndexOf(String str, int fromIndex)
例子
public class Test {
public static void main(String args[]) {
String Str = new String("菜鸟教程:www.runoob.com");
String SubStr1 = new String("runoob");
String SubStr2 = new String("com");
System.out.print("查找字符 o 最后出现的位置 :" );
System.out.println(Str.lastIndexOf( 'o' ));
System.out.print("从第14个位置查找字符 o 最后出现的位置 :" );
System.out.println(Str.lastIndexOf( 'o', 14 ));
System.out.print("子字符串 SubStr1 最后出现的位置:" );
System.out.println( Str.lastIndexOf( SubStr1 ));
System.out.print("从第十五个位置开始搜索子字符串 SubStr1最后出现的位置 :" );
System.out.println( Str.lastIndexOf( SubStr1, 15 ));
System.out.print("子字符串 SubStr2 最后出现的位置 :" );
System.out.println(Str.lastIndexOf( SubStr2 ));
}
}
//结果
//查找字符 o 最后出现的位置 :17
//从第14个位置查找字符 o 最后出现的位置 :13
//子字符串 SubStr1 最后出现的位置:9
//从第十五个位置开始搜索子字符串 SubStr1最后出现的位置 :9
//子字符串 SubStr2 最后出现的位置 :16
3、split()
split() 方法根据匹配给定的正则表达式来拆分字符串。
注意: . 、 $、 | 和 ***** 等转义字符,必须得加 **\**。
**注意:**多个分隔符,可以用 | 作为连字符。
public String[] split(String regex, int limit)
参数
- regex -- 正则表达式分隔符。
- limit -- 分割的份数
例子
public class Test {
public static void main(String args[]) {
String str = new String("Welcome-to-Runoob");
System.out.println("- 分隔符返回值 :" );
for (String retval: str.split("-")){
System.out.println(retval);
}
System.out.println("");
System.out.println("- 分隔符设置分割份数返回值 :" );
for (String retval: str.split("-", 2)){
System.out.println(retval);
}
System.out.println("");
String str2 = new String("www.runoob.com");
System.out.println("转义字符返回值 :" );
for (String retval: str2.split("\\.", 3)){
System.out.println(retval);
}
System.out.println("");
String str3 = new String("acount=? and uu =? or n=?");
System.out.println("多个分隔符返回值 :" );
for (String retval: str3.split("and|or")){
System.out.println(retval);
}
}
}
//结果
/*-----------
- 分隔符返回值 :
Welcome
to
Runoob
- 分隔符设置分割份数返回值 :
Welcome
to-Runoob
转义字符返回值 :
www
runoob
com
多个分隔符返回值 :
acount=?
uu =?
n=?
-----------*/
4、contains()
contains() 方法用于判断字符串中是否包含指定的字符或字符串。
public boolean contains(CharSequence chars)
参数
chars -- 要判断的字符或字符串。
例子
public class Main {
public static void main(String[] args) {
String myStr = "Runoob";
System.out.println(myStr.contains("Run"));//true
System.out.println(myStr.contains("o"));//true
System.out.println(myStr.contains("s"));//false
}
}