當前位置:才華齋>計算機>java語言>

Java中String類的方法

java語言 閱讀(2.77W)

String類用於描述字串事物的,那麼它就提供了多個方法對字串進行操作方法都會用,以下就是小編精心推薦Java中String類的方法,希望對大家有幫助!

Java中String類的方法

一、建構函式

String(byte[ ] bytes):通過byte陣列構造字串物件。

String(char[ ] value):通過char陣列構造字串物件。

String(Sting original):構造一個original的副本。即:拷貝一個original。

String(StringBuffer buffer):通過StringBuffer陣列構造字串物件。

例如:

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

tln("sb:"+sb);

tln("sb_sub:"+sb_sub);

tln("sc:"+sc);

tln("sc_sub:"+sc_sub);

tln("sb_copy:"+sb_copy);

輸出結果:sb:abcdefghij

sb_sub:de

sc:0123456789

sc_sub:34

sb_copy:abcdefghij

二、方法:

說明:①、所有方法均為public。

②、書寫格式: [修飾符] <返回型別><方法名([引數列表])>

例如:static int parseInt(String s)

表示此方法(parseInt)為類方法(static),返回型別為(int),方法所需要為String型別。

1. char charAt(int index) :取字串中的某一個字元,其中的引數index指的是字串中序數。字串的序數從0開始到length()-1 。

例如:String s = new String("abcdefghijklmnopqrstuvwxyz");

tln("At(5): " + At(5) );

結果為: At(5): f

2. int compareTo(String anotherString) :當前String物件與anotherString比較。相等關係返回0;不相等時,從兩個字串第0個字元開始比較,返回第一個不相等的字元差,另一種情況,較長字串的前面部分恰巧是較短的字串,返回它們的長度差。

3. int compareTo(Object o) :如果o是String物件,和2的功能一樣;否則丟擲ClassCastException異常。

例如:String s1 = new String("abcdefghijklmn");

String s2 = new String("abcdefghij");

String s3 = new String("abcdefghijalmn");

tln("areTo(s2): " + areTo(s2) ); //返回長度差

tln("areTo(s3): " + areTo(s3) ); //返回'k'-'a'的差

結果為:areTo(s2): 4

areTo(s3): 10

4. String concat(String str) :將該String物件與str連線在一起。

5. boolean contentEquals(StringBuffer sb) :將該String物件與StringBuffer物件sb進行比較。

6. static String copyValueOf(char[] data) :

7. static String copyValueOf(char[] data, int offset, int count) :這兩個方法將char陣列轉換成String,與其中一個建構函式類似。

8. boolean endsWith(String suffix) :該String物件是否以suffix結尾。

例如:String s1 = new String("abcdefghij");

String s2 = new String("ghij");

tln("With(s2): " + With(s2) );

結果為:With(s2): true

9. boolean equals(Object anObject) :當anObject不為空並且與當前String物件一樣,返回true;否則,返回false。

10. byte[] getBytes() :將該String物件轉換成byte陣列。

11. void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) :該方法將字串拷貝到字元陣列中。其中,srcBegin為拷貝的起始位置、srcEnd為拷貝的結束位置、字串數值dst為目標字元陣列、dstBegin為目標字元陣列的拷貝起始位置。

例如:char[] s1 = {'I',' ','l','o','v','e',' ','h','e','r','!'};//s1=I love her!

String s2 = new String("you!"); hars(0,3,s1,7); //s1=I love you!

tln( s1 );

結果為:I love you!

12. int hashCode() :返回當前字元的雜湊表碼。

13. int indexOf(int ch) :只找第一個匹配字元位置。

14. int indexOf(int ch, int fromIndex) :從fromIndex開始找第一個匹配字元位置。

15. int indexOf(String str) :只找第一個匹配字串位置。

16. int indexOf(String str, int fromIndex) :從fromIndex開始找第一個匹配字串位置。

例如:String s = new String("write once, run anywhere!");

String ss = new String("run");

tln("xOf('r'): " + xOf('r') );

tln("xOf('r',2): " + xOf('r',2) );

tln("xOf(ss): " + xOf(ss) );

結果為:xOf('r'): 1

xOf('r',2): 12

xOf(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) 以上四個方法與13、14、15、16類似,不同的是:找最後一個匹配的內容。

public class CompareToDemo {

public static void main (String[] args) {

String s1 = new String("acbdebfg");

tln(IndexOf((int)'b',7));

}

}

執行結果:5

(其中fromIndex的`引數為 7,是從字串acbdebfg的最後一個字元g開始往前數的位數。既是從字元c開始匹配,尋找最後一個匹配b的位置。所以結果為 5)

21. int length() :返回當前字串長度。

22. String replace(char oldChar, char newChar) :將字元號串中第一個oldChar替換成newChar。

23. boolean startsWith(String prefix) :該String物件是否以prefix開始。

24. boolean startsWith(String prefix, int toffset) :該String物件從toffset位置算起,是否以prefix開始。

例如:String s = new String("write once, run anywhere!");

String ss = new String("write");

String sss = new String("once");

tln("tsWith(ss): " + tsWith(ss) );

tln("tsWith(sss,6): " + tsWith(sss,6) );

結果為:tsWith(ss): true

tsWith(sss,6): true

25. String substring(int beginIndex) :取從beginIndex位置開始到結束的子字串。

ng substring(int beginIndex, int endIndex) :取從beginIndex位置開始到endIndex位置的子字串。

27. char[ ] toCharArray() :將該String物件轉換成char陣列。

28. String toLowerCase() :將字串轉換成小寫。

29. String toUpperCase() :將字串轉換成大寫。

例如:String s = new String("s String");

tln("perCase(): " + perCase() );

tln("werCase(): " + werCase() );

結果為:perCase(): S STRING

werCase(): s 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)

以上方法用於將各種不同型別轉換成Java字元型。這些都是類方法。

Java中String類的常用方法:

public char charAt(int index)

返回字串中第index個字元;

public int length()

返回字串的長度;

public int indexOf(String str)

返回字串中第一次出現str的位置;

public int indexOf(String str,int fromIndex)

返回字串從fromIndex開始第一次出現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)

判斷一個字串是否以suffix字串結尾;

public String toUpperCase()

返回一個字串為該字串的大寫形式;

public String toLowerCase()

返回一個字串為該字串的小寫形式

public String substring(int beginIndex)

返回該字串從beginIndex開始到結尾的子字串;

public String substring(int beginIndex,int endIndex)

返回該字串從beginIndex開始到endsIndex結尾的子字串

public String trim()

返回該字串去掉開頭和結尾空格後的字串

public String[] split(String regex)

將一個字串按照指定的分隔符分隔,返回分隔後的字串陣列

例項:

public class SplitDemo{

public static void main (String[] args) {

String date = "2008/09/10";

String[ ] dateAfterSplit= new String[3];

dateAfterSplit=t("/"); //以“/”作為分隔符來分割date字串,並把結果放入3個字串中。

for(int i=0;i<th;i++)< p="">

t(dateAfterSplit[i]+" ");

}

}

執行結果:2008 09 10 //結果為分割後的3個字串

例項:

:

程式程式碼

public class TestString1

{

public static void main(String args[]) {

String s1 = "Hello World" ;

String s2 = "hello world" ;

tln(At(1)) ;

tln(th()) ;

tln(xOf("World")) ;

tln(xOf("World")) ;

tln(ls(s2)) ;

tln(lsIgnoreCase(s2)) ;

String s = "我是J2EE程式設計師" ;

String sr = ace('我','你') ;

tln(sr) ;

}

}

:

程式程式碼

public class TestString2

{

public static void main(String args[]) {

String s = "Welcome to Java World!" ;

String s2 = " magci " ;

tln(tsWith("Welcome")) ;

tln(With("World")) ;

String sL = werCase() ;

String sU = perCase() ;

tln(sL) ;

tln(sU) ;

String subS = tring(11) ;

tln(subS) ;

String s1NoSp = () ;

tln(s1NoSp) ;

}