JAVA中擷取字串substring用法詳解 | java substring
substringpublicStringsubstring(intbeginIndex)返回一個新的字串,它是此字串的一個子字串。該子字串始於指定索引處的字元,一直到此字串末尾。例如:"unhappy".substring(2)returns"happy""Harbison".substring(3)returns"bison""emptiness".substring(9)returns""(anemptystring)引數:beginIndex–開始處的索引(包括)。返回:指定的子字串。丟擲:IndexOutOfBoundsException–如果beginIndex為負或大於此String物件的長度。substringpublicStringsubstring(intbeginIndex,intendIndex)返回一個新字串,它是此字串的一個子字串。該子...
substring
public String substring(int beginIndex)
返回一個新的字串,它是此字串的一個子字串。該子字串始於指定索引處的字元,一直到此字串末尾。
例如:
"unhappy".substring(2) returns "happy" "Harbison".substring(3) returns "bison" "emptiness".substring(9) returns "" (an empty string)引數:
beginIndex – 開始處的索引(包括)。
返回:
指定的子字串。
丟擲:
IndexOutOfBoundsException – 如果 beginIndex 為負或大於此 String 物件的長度。
substring
public String substring(int beginIndex, int endIndex)
返回一個新字串,它是此字串的一個子字串。該子字串從指定的 beginIndex 處開始, endIndex:到指定的 endIndex-1處結束。
示例:
"hamburger".substring(3,8) returns "burge" "smiles".substring(0,5) returns "smile"
引數:
beginIndex – 開始處的索引(包括)。
endindex 結尾處索引(不包括)。
返回:
指定的子字串。
丟擲:
IndexOutOfBoundsException – 如果 beginIndex 為負,或length大於字串長度。
示例
var str="Hello world!"document.write(str.substring(1,3));
上面返回字串:”el”;
str.substring(1,2) //返回e
str.substring(1) //返回”ello world”;
還有此函式中會出現...