java String类的api ----学习日记
1.length
返回字符串的长度.
2.toCharArray()
字符串转char类型.
3.charAt()
返回指定索引处的字符。索引范围为从 0 到 length() - 1.
4.isEmpty()
判断字符串是否为空.
5.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。
6.equalsIgnoreCase()
equalsIgnoreCase() 方法用于将字符串与指定的对象比较,不考虑大小写。
7.substring()
方法返回字符串的子字符串。

1 public class RunoobTest { 2 public static void main(String args[]) { 3 String Str = new String("This is text"); 4 5 System.out.print("返回值 :" ); 6 System.out.println(Str.substring(4) ); 7 8 System.out.print("返回值 :" ); 9 System.out.println(Str.substring(4, 10) ); 10 } 11 }
8.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 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
9.replace()
方法通过用 newChar 字符替换字符串中出现的所有 searchChar 字符,并返回替换后的新字符串。
public class Main { public static void main(String args[]) { String Str = new String("Runoob"); System.out.print("返回值 :" ); System.out.println(Str.replace('o', 'T')); System.out.print("返回值 :" ); System.out.println(Str.replace('u', 'D')); } } 以上程序执行结果为: 返回值 :RunTTb 返回值 :RDnoob
10.split()
split() 方法根据匹配给定的正则表达式来拆分字符串。
注意: . 、 $、 | 和 * 等转义字符,必须得加 \\。
注意:多个分隔符,可以用 | 作为连字符。
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=?

浙公网安备 33010602011771号