不知道你說(shuō)的實(shí)現(xiàn)是怎么個(gè)實(shí)現(xiàn)?如果只是調(diào)用的話(huà) str.subString(begin,end)就可以了,如果是java的底層實(shí)現(xiàn)的話(huà),下面是來(lái)自java.lang.String的代碼:
| public String substring( int beginIndex, int endIndex) { if (beginIndex < 0 ) { throw new StringIndexOutOfBoundsException(beginIndex); } if (endIndex > count) { throw new StringIndexOutOfBoundsException(endIndex); } if (beginIndex > endIndex) { throw new StringIndexOutOfBoundsException(endIndex - beginIndex); } return ((beginIndex == 0 ) && (endIndex == count)) ? this : new String(offset + beginIndex, endIndex - beginIndex, value); } |
然后調(diào)用String的一個(gè)私有構(gòu)造器:
| // Package private constructor which shares value array for speed. String( int offset, int count, char value[]) { this .value = value; this .offset = offset; this .count = count; } |