Java String的常用方法

2021-07-07 03:01:17 字數 4738 閱讀 3431

public char charat(int index)

返回指定索引處的字元。
public int compareto(string anotherstring)

public int comparetoignorecase(string str)

比較兩個字串的字典順序。

比較兩個字串按字典順序,不區分大小寫的差異。

1) 相等關係返回0。

2) 不相等時,從兩個字串第0個字元開始比較,返回第乙個不相等的字元差:this.charat(k)-anotherstring.charat(k)。

3) 較長字串的前面部分恰巧是較短的字串,返回它們的長度差:this.length()-anotherstring.length()。

public string concat(string str)

將指定的字串串連到這個字串的結尾。
public boolean contains(charsequence s)

返回true當且僅當此字串包含char值的特定序列。
public boolean endswith(string suffix)

測試此字串是否以suffix字尾結束。
public boolean equals(object anobject)

當anobject不為空並且與當前string物件一樣,返回true;否則,返回false。
public boolean equalsignorecase(string anotherstring)

比較該字串與anotherstring,不考慮大小寫。
public byte getbytes()

將該string物件轉換成byte陣列。
public void getchars(int srcbegin, int srcend, char dst, int dstbegin)

複製字元從該字串到目標字元陣列。

srcbegin為拷貝的起始位置,srcend為拷貝的結束位置,dst為目標字元陣列,dstbegin為目標字元陣列的拷貝起始位置。

public int hashcode()

返回此字串的雜湊碼。
public int indexof(int ch)

public int indexof(int ch, int fromindex)

返回此字串指定字元第一次出現處的索引。
public int indexof(string str)

public int indexof(string str, int fromindex)

返回此字串指定子字串第一次出現處的索引。
public string intern()

返回字串物件的規範表示。
public boolean isempty()

返回true當且僅當此字串長度為0。
public int lastindexof(int ch)

public int lastindexof(int ch, int fromindex)

返回此字串指定字元最後一次出現處的索引。
public int lastindexof(string str)

public int lastindexof(string str, int fromindex)

返回此字串指定子字串最後一次出現處的索引。
public int length()

返回此字串的長度。
public boolean matches(string regex)

此字串是否給定的正規表示式匹配。

當匹配正規表示式的特殊字元時,需加"\\"轉義。

* . ? + $ ^ [ ] ( ) | \ /

public string replace(char oldchar, char newchar)

返回此字串中使用newchar替換oldchar產生的乙個新的字串。
public string replace(charsequence target, charsequence replacement)

返回此字串中使用replacement替換charsequence產生的乙個新的字串。
public string replaceall(string regex, string replacement)

使用replacement替換此字串匹配給定的正規表示式的每個子字串。

pattern.compile(regex).matcher(str).replaceall(repl)

public string replacefirst(string regex, string replacement)

使用replacement替換此字串中第乙個匹配給定的正規表示式的子字串。

pattern.compile(regex).matcher(str).replacefirst(repl)

public string split(string regex)

public string split(string regex, int limit)

分割此字串圍繞匹配給定的正規表示式。

當匹配正規表示式的特殊字元時,需加"\\"轉義。

* . ? + $ ^ [ ] ( ) | \ /

public boolean startswith(string prefix)

public boolean startswith(string prefix, int toffset)

測試此字串是否以prefix字首開始。
public charsequence subsequence(int beginindex, int endindex)

返回乙個新的字串行,它是此序列的子串行。
public string substring(int beginindex)

public string substring(int beginindex, int endindex)

返回乙個新字串,它是此字串的乙個子字串。

新字串的length為:endindex - beginindex。

public char tochararray()

將此字串轉換為乙個新的字元陣列。
public string tolowercase()

public string tolowercase(locale locale)

將此字串中的所有字元使用預設語言環境的規則小寫。
public string tostring()

該物件本身返回。
public string touppercase()

public string touppercase(locale locale)

將此字串中的所有字元使用預設語言環境的規則大寫。
public string trim()

返回字串的乙個副本,忽略字串開頭和結尾的空白(空格)。
public static string valueof(t t)

將引數轉為字串。

JAVA String類的常用方法

scanner cin new scanner system.in string s cin.nextline int t s.length char s s.charat 0 compareto 的返回值是int,它是先比較對應字元的大小 ascii碼順序 1 如果字串相等返回值0 2 如果第乙個...

Java String類的常用方法

1,把這個字串和另乙個字串比較 int compareto object o 2,按字典順序比較兩個字串s1compareto s2 返回值是整數型別 按字典順序比較兩個字串,不考慮大小寫 s1 comparetoignorecase str 3,將指定字串連線到字串的末尾 s1.concat s2...

JAVA string常用的操作

class stringmethoddemo public static void method sub public static void method split public static void method replace public static void method trans...