String类的常用方法
上一篇 / 下一篇 2013-12-19 21:58:15 / 个人分类:java
本章目标
掌握String类中的常用方法
了解API文档的查找方法
}+t&b7XT|)H A0
字符串替换: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数组变为字符串 } } ; ;qc ~4p#{'VK8h0 |
`^O.X@!H+L Jd0
查找指定的字符串是否存在
从头开始查找: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"
51Testing软件测试网 n ~@%?{m ^
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个位置开始查找
System.out.println(str1.indexOf("x")) ; //没有查找到返回-1 } } ; |
输出结果:
2 7 -1 |
51Testing软件测试网(u;k]^:v+vR_-Po
不区分大小写的比较:
equals():只比较大小写一样的字符
public boolean
equalsIgnoreCase(String anotherString)
public class StringAPIDemo11{ public static void main(String
args[]){ String str1 =
"helloWORLD" ; //声明字符串 String str2 =
"HELLOworld" ; //声明字符串
System.out.println("\"helloWORLD\" equalsIgnoreCase
\"helloworld\" " + str1.equalsIgnoreCase(str2)) ;
System.out.println("\"helloWORLD\" equals
\"helloworld\" " + str1.equals(str2)) ; } } ; |
输出结果:
Subtopic |
51Testing软件测试网jZ}%Vo6F sZ
判断是否以指定的字符串开关或结尾
判断是否以指定的字符串开头:public boolean startsWith(String
prefix)
判断是否以指定的字符串结尾:public boolean
endsWith(String suffix)
public class StringAPIDemo10{ public static void main(String
args[]){ String str1 = "**hello"
; //声明字符串 String str2 =
"hello**" ; //声明字符串
if(str1.startsWith("**")){//判断是否以"**"开头
System.out.println("(**HELLO)以**开头") ; }
if(str2.endsWith("**")){//判断是否以"**"结尾
System.out.println("(**HELLO)以**结尾") ; } } } ; |
输出结果:
(**HELLO)以**开头 (**HELLO)以**结尾 |
!L9P-`7w!a T0
拆分字符串:public String[] split(String
regex,int limit)
public class StringAPIDemo09{ public static void main(String
args[]){ String str1 =
"hello,luoriver" ; //声明字符串
System.out.println("将\"hello
world\"转换成大写:" + str1.toUpperCase()) ;
System.out.println("将\"HELLO
WORLD\"转换成小写:" + str1.toLowerCase()) ; } } ; |
输出结果:
将"hello
world"转换成大写:HELLO,LUORIVER 将"HELLO WORLD"转换成小写:hello,luoriver |
&_.Vr^2Wz0lk~X&L3`0
字符截取
从指定位置开始一直截取到最后:public String substring(int
beginIndex)
截取指定范围的字符串:public String substring(int
beginIndex,int endIndex)
public class StringAPIDemo07{ public static void main(String
args[]){ String str1 =
"hello,luoriver" ; //声明字符串
System.out.println(str1.substring(2)) ; //查找返回位置 System.out.println(str1.substring(3,7))
;
System.out.println(str1.substring(22)) ; } } ; |
输出结果:
llo,luoriver lo,l Exception in thread
"main" java.lang.StringIndexOutOfBoundsException: String ind ex out of range: -8 at java.lang.String.substring(Unknown
Source) at
StringAPIDemo07.main(StringAPIDemo07.java:6) |
51Testing软件测试网!q|6v!AHz S.b
,m.x$D0EN.e7{?&|+f0
去掉空格
public class StringAPIDemo06{ public static void main(String
args[]){ String str1 = " hello,luoriver " ; //声明字符串
System.out.println(str1) ; //查找返回左右位置
System.out.println(str1.trim()) ;
//去掉空格 } } ; |
输出结果:
hello,luoriver hello,luoriver |
H(S }E;V`FR0
字符串与byte数组的转换
字符串变为字节数组:public byte[] getBytes()
public class StringAPIDemo03{ public static void main(String
args[]){ String str1 = "hello" ;
//定义String对象 byte b[] =
str1.getBytes() ; //将字符串变为byte
System.out.println(new String(b)) ;
//将全部的byte数组变为字符串
System.out.println(new String(b,1,3)) ; //将部分的byte数组变为字符串 } } ; |
输出结果:
hello ell |
9w x4g3fm6oe Nw0
将一个字节数组变为字符串
将全部字节数组变为String:public String(byte[]
bytes)
将部分字节数组变为String:public String(byte[]
bytes,int offset,int length)
9e Qu,a"Ds afm+D0
从字符串中取出指定位置的字符:public char charAt(int index)
public class StringAPIDemo02{ public static void main(String
args[]){ String str1 = "hello" ;
//定义String对象
System.out.println(str1.charAt(3)) ;
//取出字符串中第四个字符 } } |
输出结果:l
$]p Lw3@$V0
将字符串变为字符数组:public char[] to CharArray()
public class StringAPIDemo01{ public static void main(String
args[]){ String str1 = "hello" ;
//字义字符串 char c[] =
str1.toCharArray() ; //将一个字符串变为一个数组 for(int i=0; i<c.length;i++){ //循环输出
System.out.print(c[i] + "、") ; } } } |
D1oVbB|+W2T!z0
F,Lv\ Gz;Q Ik0
将字符数组变为字符串
public String(char[]
value,int offset, int count)
public String(char[]
value)
public class StringAPIDemo01{ public static void main(String
args[]){ String str1 = "hello" ;
//字义字符串 char c[] =
str1.toCharArray() ; //将一个字符串变为一个数组 for(int i=0;
i<c.length;i++){ //循环输出
System.out.print(c[i] + "、") ; }
System.out.println("") ; //换行 String str2 = new
String(c) ; //将全部的字符数组变为String String str3 = new
String(c,0,3) ; //将部分字符数组变为为字符串 System.out.println(str2)
; //输出字符串
System.out.println(str3) ;
//输出字符串 } } |