String类的常用方法
上一篇 / 下一篇 2013-12-19 21:58:15 / 个人分类:java
本章目标
掌握String类中的常用方法
了解API文档的查找方法
51Testing软件测试网 F_4ZSOyUU
字符串替换:public String replaceAll(String
regex,String replacement)
public class StringAPIDemo12{ public static void main(String
args[]){ String str1 = "hello" ;
//声明字符串 String newStr =
str1.replaceAll("l","x") ; //现在将所有的“l”替换成x
System.out.println("hello现在显示为" + newStr)
; } } ; |
取得一个字符串的长度:public int length()
public class
StringAPIDemo04{ public static void main(String
args[]){ String str1 = "hello
luoriver" ; //定义String对象 int n =
str1.length() ;
System.out.println(n) ; //将部分的byte数组变为字符串 } } ; vQp7V2t4P\0 |
51Testing软件测试网}D;d1c'X{)neP:nl
查找指定的字符串是否存在
从头开始查找:public int indexOf(String str)
public class StringAPIDemo05{ public static void main(String
args[]){ String str1 =
"abcdefgcgh" ; //声明字符串
System.out.println(str1.indexOf("c")) ; //查找返回位置
System.out.println(str1.indexOf("c",3)) ; //查找返回位置,从第4个位置开始查找 } } ; |
从指定位置开始查找:public int indexOf(String str,int
fromIndex)
查找的时候方法的返回值是一个int类型的数据,此数据表示的是一个字符串的具体位置,如果没有查找到此字符串,则返回"-1"