1、[/size]
[size=5]
[/size]
[size=5]String類常用方法
2
1。2、求字串某一位置字元
public char charAt(int index)//返回字串中指定位置的字元;注意字串中第一個字元索引是0,最後一個是length()-1。
String str = new String(“asdfzxc”);
char ch = str。charAt(4);//ch = z12
1
2
1。3、提取子串
用String類的substring方法可以提取字串中的子串,該方法有兩種常用引數:
1)public String substring(int beginIndex)//該方法從beginIndex位置起,從當前字串中取出剩餘的字元作為一個新的字串返回。
2)public String substring(int beginIndex, int endIndex)//該方法從beginIndex位置起,從當前字串中取出到endIndex-1位置的字元作為一個新的字串返回。
String str1 = new String(“asdfzxc”);
String str2 = str1。substring(2);//str2 = “dfzxc”
String str3 = str1。substring(2,5);//str3 = “dfz” 123
1
2
3
1。4、字串比較
1)public int compareTo(String anotherString)//該方法是對字串內容按字典順序進行大小比較,透過返回的整數值指明當前字串與引數字串的大小關係。若當前物件比引數大則返回正整數,反之返回負整數,相等返回0。
2)public int compareToIgnore(String anotherString)//與compareTo方法相似,但忽略大小寫。
3)public boolean equals(Object anotherObject)//比較當前字串和引數字串,在兩個字串相等的時候返回true,否則返回false。
4)public boolean equalsIgnoreCase(String anotherString)//與equals方法相似,但忽略大小寫。
String str1 = new String(“abc”);
String str2 = new String(“ABC”);
int a = str1。compareTo(str2);//a>0
int b = str1。compareTo(str2);//b=0
boolean c = str1。equals(str2);//c=false
boolean d = str1。equalsIgnoreCase(str2);//d=true123456
1
2
1。5、字串連線
public String concat(String str)//將引數中的字串str連線到當前字串的後面,效果等價於”+”。
String str = “aa”。concat(“bb”)。concat(“cc”); 1
1
相當於String str = “aa”+”bb”+”cc”;
1。6、字串中單個字元查詢
1)public int indexOf(int ch/String str)//用於查詢當前字串中字元或子串,返回字元或子串在當前字串中從左邊起首次出現的位置,若沒有出現則返回-1。
2)public int indexOf(int ch/String str, int fromIndex)//
改
方法與第一種類似,區別在於該方法從fromIndex位置向後查詢。
3)public int lastIndexOf(int ch/String str)//該方法與第一種類似,區別在於該方法從字串的末尾位置向前查詢。
4)public int lastIndexOf(int ch/String str, int fromIndex)//該方法與第二種方法類似,區別於該方法從fromIndex位置向前查詢。
String str = “I am a good student”;
int a = str。indexOf(‘a’);//a = 2
int b = str。indexOf(“good”);//b = 7
int c = str。indexOf(“w”,2);//c = -1
int d = str。lastIndexOf(“a”);//d = 5
int e = str。lastIndexOf(“a”,3);//e = 2123456
1
2
3
1。7、字串中字元的大小寫轉換
1)public String toLowerCase()//返回將當前字串中所有字元轉換成小寫後的新串
2)public String toUpperCase()//返回將當前字串中所有字元轉換成大寫後的新串
String str = new String(“asDF”);
String str1 = str。toLowerCase();//str1 = “asdf”
String str2 = str。toUpperCase();//str2 = “ASDF” 123
1
2
3
1。8、字串中字元的替換
1)public String replace(char oldChar, char newChar)//用字元newChar替換當前字串中所有的oldChar字元,並返回一個新的字串。
2)public String replaceFirst(String regex, String replacement)//該方法用字元replacement的內容替換當前字串中遇到的第一個和字串regex相匹配的子串,應將新的字串返回。
3)public String replaceAll(String regex, String replacement)//該方法用字元replacement的內容替換當前字串中遇到的所有和字串regex相匹配的子串,應將新的字串返回。
String str = “asdzxcasd”;
String str1 = str。replace(‘a’,‘g’);//str1 = “gsdzxcgsd”
String str2 = str。replace(“asd”,“fgh”);//str2 = “fghzxcfgh”
String str3 = str。replaceFirst(“asd”,“fgh”);//str3 = “fghzxcasd”
String str4 = str。replaceAll(“asd”,“fgh”);//str4 = “fghzxcfgh” 12345
1
2
3
String str = “ a sd ”;
String str1 = str。trim();
int a = str。length();//a = 6
int b = str1。length();//b = 4 1234
1
2
3
4
//用來比較當前字串的起始字元或子字串prefix和終止字元或子字串suffix是否和當前字串相同,過載方法中同時還可以指定比較的開始位置offset。
2)boolean statWith(String prefix)或boolean endWith(String suffix)
String str = “asdfgh”;
boolean a = str。statWith(“as”);//a = true
boolean b = str。endWith(“gh”);//b = true 123
1
2
3//從當前字串的firstStart位置開始比較,取長度為length的一個子字串,other字串從otherStart位置開始,指定另外一個長度為length的字串,兩字串比較,當b為true時字串不區分大小寫。
3)regionMatches(boolean b, int firstStart, String other, int otherStart, int length)
4)contains(String str)//判斷引數s是否被包含在字串中,並返回一個
布林
型別的值。
String str = “student”;
str。contains(“stu”);//true
str。contains(“ok”);//false 123
1
2
3
5)String[] split(String str)//將str作為分隔符進行字串分解,分解後的字字串在字串陣列中返回。
String str = “asd!qwe|zxc#”;
String[] str1 = str。split(“!|#”);//str1[0] = “asd”;str1[1] = “qwe”;str1[2] = “zxc”; 12
1
2
3
indexOf(String str):獲取str在字串物件中第一次出現的索引
String str = “adsfaxsdfas沙發上案發地方”;
System。out。println(str。indexOf(‘a’,5));
1
2
執行結果:
94)substring(int start):從start開始擷取字串
String str = “adsfaxsdfas沙發上案發地方”;
System。out。println(str。substring(1));
1
2
執行結果:
dsfaxsdfas沙發上發地方
5)String substring(int start,int end):從start開始,到end結束擷取字串。包括start,不包括
1endString str = “adsfaxsdfas沙發上案發地方”;
System。out。println(str。substring(1, 12));
2
執行結果:
dsfaxsdfas沙
String判斷功能1)[/size]
[size=5]
equals(Object obj):比較字串的內容是否相同
String str = “adsfaxsdfas沙發上案發地方”;
1System。out。println(str。equals(“adsfaxsdfas沙發上發地方”));
2System。out。println(str。equals(“adsfaxsdfas”));
3
執行結果:
true
false2)
equalsIgnoreCase(String anotherString):比較字串的內容是否相同,忽略大小寫
1String str = “adsfaxsdfas沙發上案發地方”;
2System。out。println(str。equalsIgnoreCase(“ADsfaxsdfAs沙發上發地方”));
執行結果:
true3)
startsWith(String prefix):判斷字串物件是否以指定的字元開頭(區分大小寫)
2String str = “adsfaxsdfas沙發上案發地方”;
System。out。println(str。startsWith(“a”));
System。out。println(str。startsWith(“A”));
1
3
執行結果:
true
false4)
startsWith(String prefix,int toffset):判斷字串物件是否以指定的字元開頭,引數toffset為指定從哪個下標開始
String str = “adsfaxsdfas沙發上案發地方”;
System。out。println(str。startsWith(“f”, 3));
System。out。println(str。startsWith(“f”, 4));
1
2
3
執行結果:
true
false5)
endsWith(String str):判斷字串物件是否以指定的字元結尾
String str = “adsfaxsdfas沙發上案發地方”;
System。out。println(str。endsWith(“x”));
System。out。println(str。endsWith(“方”));
1
2
3
執行結果:
false
true6)
isEmpty():判斷指定字串是否為空7)startsWith(String prefix):判斷字串是否以指定字元開頭
String str = “adsfaxsdfas沙發上發地方”;
System。out。println(str。startsWith(“a”));
1
2
執行結果:
true4。
toLowerCase():把字串轉換為小寫字串
String str1 = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
1System。out。println(str1。toLowerCase());
2
執行結果:
abcdefghijklmnopqrstuvwxyz3)
toUpperCase():把字串轉換為大寫字串
String str1 = “abcdefghijklmnopqrstuvwxyz”;
System。out。println(str2。toUpperCase());
1
2
執行結果:
ABCDEFGHIJKLMNOPQRSTUVWXYZ5。
其他常用方法1)trim():去除字串兩端空格
String str3 = “ a c e x u a n x u a n ”;
1
2System。out。println(str3。trim());System。out。println(str3);
3執行結果:a c e x u a n x u a n a c e x u a n x u a n
6
7
2)split():去除字串中指定的的字元,然後返回一個新的字串
public static void main(String[ args) {
String str = “adsfaxsdfas沙發上發地方”;
String array[ = str。split(“a”);
printString(array);
}
public static void printString(String a[) {