String : 字符串類型
一、構(gòu)造函數(shù)
String(byte[ ] bytes):通過byte數(shù)組構(gòu)造字符串對(duì)象。
String(char[ ] value):通過char數(shù)組構(gòu)造字符串對(duì)象。
String(Sting original):構(gòu)造一個(gè)original的副本。即:拷貝一個(gè)original。
String(StringBuffer buffer):通過StringBuffer數(shù)組構(gòu)造字符串對(duì)象。
例如:
byte[] b = {'a','b','c','d','e','f','g','h','i','j'};
char[] c = {'0','1','2','3','4','5','6','7','8','9'};
String sb = new String(b); //abcdefghij
String sb_sub = new String(b,3,2); //de
String sc = new String(c); //0123456789
String sc_sub = new String(c,3,2); //34
String sb_copy = new String(sb); //abcdefghij
System.out.println("sb:"+sb);
System.out.println("sb_sub:"+sb_sub);
System.out.println("sc:"+sc);
System.out.println("sc_sub:"+sc_sub);
System.out.println("sb_copy:"+sb_copy);
輸出結(jié)果:sb:abcdefghij
sb_sub:de
sc:0123456789
sc_sub:34
sb_copy:abcdefghij
二、方法:
說(shuō)明:①、所有方法均為public。
②、書寫格式: [修飾符] <返回類型><方法名([參數(shù)列表])>
例如:static int parseInt(String s)
表示此方法(parseInt)為類方法(static),返回類型為(int),方法所需要為String類型。
1. char charAt(int index) :取字符串中的某一個(gè)字符,其中的參數(shù)index指的是字符串中序數(shù)。字符串的序數(shù)從0開始到length()-1 。
例如:String s = new String("abcdefghijklmnopqrstuvwxyz");
System.out.println("s.charAt(5): " + s.charAt(5) );
結(jié)果為: s.charAt(5): f
2. int compareTo(String anotherString) :當(dāng)前String對(duì)象與anotherString比較。相等關(guān)系返回0;不相等時(shí),從兩個(gè)字符串第0個(gè)字符開始比較,返回第一個(gè)不相等的字符差,另一種情況,較長(zhǎng)字符串的前面部分恰巧是較短的字符串,返回它們的長(zhǎng)度差。
3. int compareTo(Object o) :如果o是String對(duì)象,和2的功能一樣;否則拋出ClassCastException異常。
例如:String s1 = new String("abcdefghijklmn");
String s2 = new String("abcdefghij");
String s3 = new String("abcdefghijalmn");
System.out.println("s1.compareTo(s2): " + s1.compareTo(s2) ); //返回長(zhǎng)度差
System.out.println("s1.compareTo(s3): " + s1.compareTo(s3) ); //返回'k'-'a'的差
結(jié)果為:s1.compareTo(s2): 4
s1.compareTo(s3): 10
4. String concat(String str) :將該String對(duì)象與str連接在一起。
5. boolean contentEquals(StringBuffer sb) :將該String對(duì)象與StringBuffer對(duì)象sb進(jìn)行比較。
6. static String copyValueOf(char[] data) :
7. static String copyValueOf(char[] data, int offset, int count) :這兩個(gè)方法將char數(shù)組轉(zhuǎn)換成String,與其中一個(gè)構(gòu)造函數(shù)類似。
8. boolean endsWith(String suffix) :該String對(duì)象是否以suffix結(jié)尾。
例如:String s1 = new String("abcdefghij");
String s2 = new String("ghij");
System.out.println("s1.endsWith(s2): " + s1.endsWith(s2) );
結(jié)果為:s1.endsWith(s2): true
9. boolean equals(Object anObject) :當(dāng)anObject不為空并且與當(dāng)前String對(duì)象一樣,返回true;否則,返回false。
10. byte[] getBytes() :將該String對(duì)象轉(zhuǎn)換成byte數(shù)組。
11. void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) :該方法將字符串拷貝到字符數(shù)組中。其中,srcBegin為拷貝的起始位置、srcEnd為拷貝的結(jié)束位置、字符串?dāng)?shù)值dst為目標(biāo)字符數(shù)組、dstBegin為目標(biāo)字符數(shù)組的拷貝起始位置。
例如:char[] s1 = {'I',' ','l','o','v','e',' ','h','e','r','!'};//s1=I love her!
String s2 = new String("you!"); s2.getChars(0,3,s1,7); //s1=I love you!
System.out.println( s1 );
結(jié)果為:I love you!
12. int hashCode() :返回當(dāng)前字符的哈希表碼。
13. int indexOf(int ch) :只找第一個(gè)匹配字符位置。
14. int indexOf(int ch, int fromIndex) :從fromIndex開始找第一個(gè)匹配字符位置。
15. int indexOf(String str) :只找第一個(gè)匹配字符串位置。
16. int indexOf(String str, int fromIndex) :從fromIndex開始找第一個(gè)匹配字符串位置。
例如:String s = new String("write once, run anywhere!");
String ss = new String("run");
System.out.println("s.indexOf('r'): " + s.indexOf('r') );
System.out.println("s.indexOf('r',2): " + s.indexOf('r',2) );
System.out.println("s.indexOf(ss): " + s.indexOf(ss) );
結(jié)果為:s.indexOf('r'): 1
s.indexOf('r',2): 12
s.indexOf(ss): 12
17. int lastIndexOf(int ch)
18. int lastIndexOf(int ch, int fromIndex)
19. int lastIndexOf(String str)
20. int lastIndexOf(String str, int fromIndex) 以上四個(gè)方法與13、14、15、16類似,不同的是:找最后一個(gè)匹配的內(nèi)容。
public class CompareToDemo {
public static void main (String[] args) {
String s1 = new String("acbdebfg");
System.out.println(s1.lastIndexOf((int)'b',7));
}
}
運(yùn)行結(jié)果:5
(其中fromIndex的參數(shù)為 7,是從字符串a(chǎn)cbdebfg的最后一個(gè)字符g開始往前數(shù)的位數(shù)。既是從字符c開始匹配,尋找最后一個(gè)匹配b的位置。所以結(jié)果為 5)
21. int length() :返回當(dāng)前字符串長(zhǎng)度。
22. String replace(char oldChar, char newChar) :將字符號(hào)串中第一個(gè)oldChar替換成newChar。
23. boolean startsWith(String prefix) :該String對(duì)象是否以prefix開始。
24. boolean startsWith(String prefix, int toffset) :該String對(duì)象從toffset位置算起,是否以prefix開始。
例如:String s = new String("write once, run anywhere!");
String ss = new String("write");
String sss = new String("once");
System.out.println("s.startsWith(ss): " + s.startsWith(ss) );
System.out.println("s.startsWith(sss,6): " + s.startsWith(sss,6) );
結(jié)果為:s.startsWith(ss): true
s.startsWith(sss,6): true
25. String substring(int beginIndex) :取從beginIndex位置開始到結(jié)束的子字符串。
26.String substring(int beginIndex, int endIndex) :取從beginIndex位置開始到endIndex位置的子字符串。
27. char[ ] toCharArray() :將該String對(duì)象轉(zhuǎn)換成char數(shù)組。
28. String toLowerCase() :將字符串轉(zhuǎn)換成小寫。
29. String toUpperCase() :將字符串轉(zhuǎn)換成大寫。
例如:String s = new String("java.lang.Class String");
System.out.println("s.toUpperCase(): " + s.toUpperCase() );
System.out.println("s.toLowerCase(): " + s.toLowerCase() );
結(jié)果為:s.toUpperCase(): JAVA.LANG.CLASS STRING
s.toLowerCase(): java.lang.class string
30. static String valueOf(boolean b)
31. static String valueOf(char c)
32. static String valueOf(char[] data)
33. static String valueOf(char[] data, int offset, int count)
34. static String valueOf(double d)
35. static String valueOf(float f)
36. static String valueOf(int i)
37. static String valueOf(long l)
38. static String valueOf(Object obj)
以上方法用于將各種不同類型轉(zhuǎn)換成Java字符型。這些都是類方法。
Java中String類的常用方法:
public char charAt(int index)
返回字符串中第index個(gè)字符;
public int length()
返回字符串的長(zhǎng)度;
public int indexOf(String str)
返回字符串中第一次出現(xiàn)str的位置;
public int indexOf(String str,int fromIndex)
返回字符串從fromIndex開始第一次出現(xiàn)str的位置;
public boolean equalsIgnoreCase(String another)
比較字符串與another是否一樣(忽略大小寫);
public String replace(char oldchar,char newChar)
在字符串中用newChar字符替換oldChar字符
public boolean startsWith(String prefix)
判斷字符串是否以prefix字符串開頭;
public boolean endsWith(String suffix)
判斷一個(gè)字符串是否以suffix字符串結(jié)尾;
public String toUpperCase()
返回一個(gè)字符串為該字符串的大寫形式;
public String toLowerCase()
返回一個(gè)字符串為該字符串的小寫形式
public String substring(int beginIndex)
返回該字符串從beginIndex開始到結(jié)尾的子字符串;
public String substring(int beginIndex,int endIndex)
返回該字符串從beginIndex開始到endsIndex結(jié)尾的子字符串
public String trim()
返回該字符串去掉開頭和結(jié)尾空格后的字符串
public String[] split(String regex)
將一個(gè)字符串按照指定的分隔符分隔,返回分隔后的字符串?dāng)?shù)組
實(shí)例:
public class SplitDemo{
public static void main (String[] args) {
String date = "2008/09/10";
String[ ] dateAfterSplit= new String[3];
dateAfterSplit=date.split("/"); //以“/”作為分隔符來(lái)分割date字符串,并把結(jié)果放入3個(gè)字符串中。
for(int i=0;i<dateAfterSplit.length;i++)
System.out.print(dateAfterSplit[i]+" ");
}
}
運(yùn)行結(jié)果:2008 09 10 //結(jié)果為分割后的3個(gè)字符串
實(shí)例:
TestString1.java:
程序代碼
public class TestString1
{
public static void main(String args[]) {
String s1 = "Hello World" ;
String s2 = "hello world" ;
System.out.println(s1.charAt(1)) ;
System.out.println(s2.length()) ;
System.out.println(s1.indexOf("World")) ;
System.out.println(s2.indexOf("World")) ;
System.out.println(s1.equals(s2)) ;
System.out.println(s1.equalsIgnoreCase(s2)) ;
String s = "我是J2EE程序員" ;
String sr = s.replace('我','你') ;
System.out.println(sr) ;
}
}
TestString2.java:
程序代碼
public class TestString2
{
public static void main(String args[]) {
String s = "Welcome to Java World!" ;
String s2 = " magci " ;
System.out.println(s.startsWith("Welcome")) ;
System.out.println(s.endsWith("World")) ;
String sL = s.toLowerCase() ;
String sU = s.toUpperCase() ;
System.out.println(sL) ;
System.out.println(sU) ;
String subS = s.substring(11) ;
System.out.println(subS) ;
String s1NoSp = s2.trim() ;
System.out.println(s1NoSp) ;
}