Java中String方法substring講解

2021-06-26 23:14:27 字數 1191 閱讀 3792

public stringsubstring(int beginindex)
返回乙個新的字串,它是此字串的乙個子字串。該子字串始於指定索引處的字元,一直到此字串末尾。

例如:

"harbison".substring(3) returns "bison"

"emptiness".substring(9) returns "" (an empty string)

引數:beginindex- 開始處的索引(包括)。

返回:指定的子字串。

丟擲:indexoutofbound***ception- 如果beginindex為負或大於此string物件的長度。

public stringsubstring(int beginindex,

int endindex)

返回乙個新字串,它是此字串的乙個子字串。該子字串從指定的beginindex處開始,一直到索引endindex - 1處的字元。因此,該子字串的長度為endindex-beginindex

示例:

"hamburger".substring(4, 8) returns "urge"

"smiles".substring(1, 5) returns "mile"

引數:beginindex- 開始處的索引(包括)。

endindex- 結束處的索引(不包括)。

返回:指定的子字串。

丟擲:indexoutofbound***ception- 如果beginindex為負,或endindex大於此string物件的長度,或beginindex大於endindex

java中String常用方法

public int length 獲取字串含有的字元個數,拿到字串長度。public string concat string str 將當前字串和引數字串拼接成為返回值新的字串。public char charat int index 獲取指定索引位置的單個字元。索引從0開始 public in...

java中String的常用方法

string的基本常用方法 這裡都是一些基本的用法,都是 舉例比較直接 字串的比較方法 返回值為boolean型別,引數為另乙個字串 if 豬八戒 equals 孫悟空 else 字串的查詢方法 返回值為int,也就是該字元在字串中的索引 引數為需要查詢的字元 string str1 android...

java中String物件的常用方法

public class test system.out.println string str1 new string c string str2 new string c,1,6 system.out.println str1 str2 system.out.println str1.charat...