Java中String類的方法
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) ;
}
-
如何使用java
Java是一種可以撰寫跨平台應用程序的面向對象的`程序設計語言。以下是小編為大家搜索整理的如何使用java,希望能給大家帶來幫助!更多精彩內容請及時關注我們應屆畢業生考試網!...
-
2017上半年計算機二級Java備考練習題及答案
練習做多了自然會形成自己的做題方法和速度,為此,今天本站小編為大家整理了以下2017上半年計算機二級Java備考練習題及答案,希望對大家有幫助!練習題一1、以下不是Java中有效的關鍵字。A、constB、NULLC、falseD、this本題考查的是Java的關鍵字。Java中常量null,fa...
-
2017上半年計算機二級Java練習題及答案
計算機等級證書是我們找工作的敲門磚,現在越來越多人重視計算機等級考試。下面是本站小編為大家帶來的2017上半年計算機二級Java練習題及答案,希望對大家的學習有幫助!一、單選題1、結構化程序設計主要強調的是______。A、程序的規模B、程序的易讀性C、程序的執...
-
java中反射機制
導語:Java是一門面向對象編程語言,不僅吸收了C++語言的各種優點,還摒棄了C++裏難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特徵。下面我們來看看java中反射機制,希望對大家有所幫助。Java中什麼是反射機制:(JavaReflactioninaction)Java反射...