public static void main(String args[]){
String s = "abcdefg";
System.out.println("0123456");
System.out.println(s);
System.out.println(s.length());
System.out.println("0-2: "+s.substring(0,2));
System.out.println("1-3: "+s.substring(1,3));
System.out.println("2: "+s.substring(2));
}
}
- - - - -
0123456 < 用來看清楚編號才加的
abcdefg < 要截取的字串源
7
0-2: ab
1-3: bc
2: cdefg
- - - - -
切割字串取其中.
屬於 class String, 其中String 繼承 Object class.[1]
java.lang.Object
java.lang.String
String |
substring(int beginIndex)
Returns a new string that is a substring of this string.
|
String |
substring(int beginIndex,
int endIndex)
Returns a new string that is a substring of this string.
|
其中, beginIndex就是要且取字串的開始,
一段字串像是一個字元陣列, 由 0 開始,
而endIndex 是結束編號, 會截取在此之前的字串.
[1] https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring%28int%29