public MyString1 substring(int begin,int end) //提取子串{}
                    
                    
                2 回答
 
                    
                    
                            慕沐林林
                            
                                
                            
                        
                        
                                                
                    TA贡献2016条经验 获得超9个赞
不知道你说的实现是怎么个实现?如果只是调用的话 str.subString(begin,end)就可以了,如果是java的底层实现的话,下面是来自java.lang.String的代码:
| publicString substring(intbeginIndex, intendIndex) {    if(beginIndex < 0) {        thrownewStringIndexOutOfBoundsException(beginIndex);    }    if(endIndex > count) {        thrownewStringIndexOutOfBoundsException(endIndex);    }    if(beginIndex > endIndex) {        thrownewStringIndexOutOfBoundsException(endIndex - beginIndex);    }    return((beginIndex == 0) && (endIndex == count)) ? this:        newString(offset + beginIndex, endIndex - beginIndex, value);    } | 
然后调用String的一个私有构造器:
| // Package private constructor which shares value array for speed.    String(intoffset, intcount, charvalue[]) {    this.value = value;    this.offset = offset;    this.count = count;    } | 
 
                    
                    
                            莫回无
                            
                                
                            
                        
                        
                                                
                    TA贡献1865条经验 获得超7个赞
给你个例子,你参考下
| publicclass$ {    publicstaticvoidmain(String[] args) {        String str = "abcdefg";        System.out.println(substring(str, 0, 1));    }    privatestaticString substring(String str, intbegin, intend) {        char[] ch = str.toCharArray();        StringBuffer buf = newStringBuffer();        for(inti = begin; i < end; i++) {            buf.append(ch[i]);        }        returnbuf.toString();    }} | 
添加回答
举报
0/150
	提交
		取消
	