2011-04-14 62 views
0

net CF子字符串函數不適用於.net cf?

我需要使用子串來捕獲字符串的中間。

txtpart.Text = "ab12345678cde"; 
string item = txtpart.Text.Substring(2,8); 

其輸出應該是12345678

但它總是拋出此異常

System.ArgumentOutOfRangeException:指定的參數已超出有效值的範圍。

除子串外還有其他函數嗎?

+0

你是指'(2,8)'? ''ab12345678cde「.Substring(3,8)'是'」2345678c「' – dtb 2011-04-14 23:51:53

回答

1

你的例子應該返回2345678c而不是12345678但它沒關係。你有的代碼是有效的,不應該拋出錯誤。你是否僅僅爲了舉例而通過ab12345678cde?你能告訴我們什麼是錯誤發生時真的被傳入嗎?

你可以看看這裏,看看你的代碼執行:

http://ideone.com/jufd3

如果你的長度過長,我相信你會得到一個錯誤,而不是你看到的一個。

0

我知道這是一個老問題,但是這可以幫助別人...

下面是一個簡單的函數,可以用來在任何字符串中間「獲取」:

public static string getBetween(string strSource, string strStart, string strEnd) 
    { 
     int Start, End; 
     if (strSource.Contains(strStart) && strSource.Contains(strEnd)) 
     { 
      Start = strSource.IndexOf(strStart, 0) + strStart.Length; 
      End = strSource.IndexOf(strEnd, Start); 
      return strSource.Substring(Start, End - Start); 
     } 
     else 
     { 
      return ""; 
     } 
    } 

,並使用它,您撥打:

string mySelectedText = getBetween(txtpart.Text, "b", "c"); 
Debug.WriteLine(mySelectedText); 

其結果將是12345678

希望它能幫助。問候