亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

請教在Java中該如何實現MyString1類中的這個函數?如下所示!

請教在Java中該如何實現MyString1類中的這個函數?如下所示!

慕無忌1623718 2021-06-30 06:07:30
public MyString1 substring(int begin,int end) //提取子串{}
查看完整描述

2 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

不知道你說的實現是怎么個實現?如果只是調用的話 str.subString(begin,end)就可以了,如果是java的底層實現的話,下面是來自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);    }

然后調用String的一個私有構造器:


// 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;    }



查看完整回答
反對 回復 2021-07-04
?
莫回無

TA貢獻1865條經驗 獲得超7個贊

給你個例子,你參考下


public class $ {    public static void main(String[] args) {         String str = "abcdefg";        System.out.println(substring(str, 01));    }     private static String substring(String str, int begin, int end) {         char[] ch = str.toCharArray();        StringBuffer buf = new StringBuffer();        for (int i = begin; i < end; i++) {            buf.append(ch[i]);        }        return buf.toString();    }}



查看完整回答
反對 回復 2021-07-04
  • 2 回答
  • 0 關注
  • 332 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號