JavaSE8基礎 String substring 返回字符串中指定索引值區間內的字符
阿新 • • 發佈:2017-09-03
索引 value true 代碼 ble bst eclipse image res
os :windows7 x64
jdk:jdk-8u131-windows-x64
ide:Eclipse Oxygen Release (4.7.0)
code:
package jizuiku.t00; public class Demo5 { public static void main(String[] args) { //索引值 012345 String str = "abc01234543210cba"; int beginIndex = 2; int endIndex = 5;//不包括這個索引所對應的字符,為啥?看看源代碼,你就知道. System.out.println(str.substring(beginIndex, endIndex)); } }
result:
scoureCode:
public String substring(int beginIndex, int endIndex) { if (beginIndex < 0) { throw new StringIndexOutOfBoundsException(beginIndex); } if (endIndex > value.length) { throw new StringIndexOutOfBoundsException(endIndex); } int subLen = endIndex - beginIndex; if (subLen < 0) { throw new StringIndexOutOfBoundsException(subLen); } return ((beginIndex == 0) && (endIndex == value.length)) ? this : new String(value, beginIndex, subLen); }
Java優秀,值得學習。
學習資源:API手冊+Java源碼。
JavaSE8基礎 String substring 返回字符串中指定索引值區間內的字符