我的代碼不知道為什么不能return,
別人說,可能是substring改變了自負串的長度
但是,我查過,并沒有改變,但是我去掉了subString的部分,果然可以retun,郁悶
謝謝你們了,以下是我的代碼
1 public static ArrayList getUrl(string Html, string linkStart, string linkEnd) 2 { 3 ArrayList urlList = new ArrayList(); 4 string tempLinkStart = "{$theLinkStart}"; 5 string tempLinkEnd = "{$theLinkEnd}"; 6 Html = Html.Replace(linkStart, tempLinkStart); 7 Html = Html.Replace(linkEnd, tempLinkEnd); 8 9 for (int i = 0; i < Html.Length; i++)10 {11 if (Html.Substring(i, tempLinkStart.Length) == tempLinkStart) //判斷是否查找到tempLinkStart({$theLinkStart})12 {13 for (int j = i + tempLinkStart.Length; j < Html.Length; j++) //從tempLinkStart({$theLinkStart})的后一位開始循環(huán)14 {15 if (Html.Substring(j, tempLinkEnd.Length) == tempLinkEnd) //判斷是否查找到tempLinkEnd({$theLinkEnd})16 {17 //把tempLinkStart({$theLinkStart})與tempLinkEnd({$theLinkEnd})之間的內容添加到urlList里18 urlList.Add(Html.Substring(i + tempLinkStart.Length, j - (i + tempLinkStart.Length)).ToString());19 //MessageBox.Show(Html.Substring(i + tempLinkStart.Length, j - (i + tempLinkStart.Length)).ToString());20 break;21 }22 }23 }24 MessageBox.Show(Html.Length.ToString()); //我這里為了測試,出數(shù)html的長度,但是沒有問題~~不變的!但是不知道為什么return不了25 }2627 return urlList;28 }
4 回答

慕森王
TA貢獻1777條經(jīng)驗 獲得超3個贊
建議你使用 Html.Indexof(tempLinkStart, i) 來進行判斷,而不要使用SubString
而且你用這個for 循環(huán),效率太低,應使用 while 循環(huán),
while (i < Html.Length)
{
int pos = Html.Indexof(tempLinkStart, i);
if (pos < 0)
{//未找到tempLinkStart
break;
}
else
{
i = pos + tempLinkStart.Length;
}
}
這樣做效率要高很多。

GCT1015
TA貢獻1827條經(jīng)驗 獲得超4個贊
=。=
估計是超出索引界限了
我記得js的substring和。net的substring處理方式不一樣
一個是從索引a到b
一個是從索引a開始的b個字符的長度
建議你try catch下
把for循環(huán)try下
看會不會到catch里
你這段代碼如果不發(fā)生死循環(huán)且不拋異常
是不會沒有retrun的

臨摹微笑
TA貢獻1982條經(jīng)驗 獲得超2個贊
說下你這個代碼: #1 效率低是一定的了,因為你這個要遍歷每一個字符 #2 代碼我測試過這個不存在return不了的問題,應該是 在11行時“if(Html.Substring(i, tempLinkStart.Length) == tempLinkStart)” 這個出現(xiàn)問題,因為當i ?本身小于html.length,但是當它再加上一個值時,就很可能出現(xiàn)超出html.length的長度,這樣就出現(xiàn)異常了 實質上是Html.Substring(i, tempLinkStart.Length)出現(xiàn)異常 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
- 4 回答
- 0 關注
- 558 瀏覽
添加回答
舉報
0/150
提交
取消