總結String類常用方法

2021-10-03 16:42:44 字數 1677 閱讀 3181

public class test 

}

1.獲取字串長度

system.out.println(str.length());//輸出6
2.判斷字串是否一致(區分大小寫)

system.out.println(str.equals(" abcd "));//輸出false
3.判斷字串是否一致(不區分大小寫)

system.out.println(str.equals(" abcd "));//輸出true
4.判斷字串是否以...開頭

system.out.println(str.stratswith(" a"));//輸出true
5.判斷字串是否以...結尾

system.out.println(str.endswith(" "));//輸出true
6.去掉字串兩端的空格

system.out.println(str.trim());//輸出abcd
7.將指定字元替換為其他字元

system.out.println(str.replaceall(" ", "a"));//輸出aabcda
8.獲取字串指定位置的字元,位置從0開始

system.out.println(str.charat(1));//輸出a
9.將字串分割為陣列中元素,遇見xx便刪去xx進行一次分割

string  x = str.split("");//x=

string y = str.split("b");//y=

10.檢索出字串中指定字元的位置

system.out.println(str.indexof(" "));//從前向後檢索,輸出0

system.out.println(str.lastindexof(" "));//從後向前檢索,輸出5

system.out.println(str.lastindexof("ab"));//遇到多個字元將其視為整體,輸出1

11.將字串中的每乙個字元解析出來組成乙個字元型陣列

char  letters = str.tochararray();

for(char letter : letters)

/*輸出: a

bcd */

12.擷取字串

system.out.println(str.substring(1));//輸出abcd  

system.out.println(str.substring(1,3));//輸出ab

總結String類常用方法

public class test string str abuiugba system.out.println str.indexof a system.out.println str.lastindexof a 從右向左數a的位置 a還是正常順序的第乙個a system.out.println ...

String類的常用方法總結

字串變為字元陣列 public char tochararray 字元陣列變為字串 使用string的構造方法實現 public string char value public string char value,int offset,int count public char charat in...

String類中的常用方法總結

package com.qfedu.b string 字串比較問題 public class demo1 注意 無論什麼時候兩個字串比較內容是否相等,我們一定要有equals方法,因為如果用的是來比較,會比較兩個字串的位址。一般只有boolean型別的資料用來進行比較 獲取字串長度方法 int le...