2012-09-20 65 views
-7

我有问题的子字符串方法,得到这个错误。在C行中使用子字符串#

“索引和长度必须引用字符串中的位置”。 “参数名:长度”
string[] nombre = item.Split(new char[]{' '}); 
this.listBox5.Items.Add(nombre[0].Substring(0,2).ToUpper()+nombre[1].Substring(0,1)); 
+2

刚转到这个错误的路线,并查看值...的 – Tigran

+0

可能重复(HTTP://计算器。 com/questions/7398238/index-and-length-must-refer-to-a-location-in-the-string) – verdesmarald

+3

解决方案:学习[debug](http://en.wikipedia.org/wiki/Debugging )。 – Adam

回答

1

这意味着要传递到Substring值不适用于他们正在呼吁字符串。例如:

string s = "hello"; 

string x = s.Substring(0, 1); // <-- This is fine (returns "h") 
string y = s.Substring(1, 3); // <-- Also fine (returns "ell") 
string z = s.Substring(5, 3); // <-- Throws an exception because 5 is passed 
           //  the end of 's' (which only has 5 characters) 

顺便说一句,我看到这个有很多:

item.Split(new char[]{' '}) 

我认为人们通过Split方法的签名混淆。以下就足够了:?索引和长度必须引用在字符串中的位置]

item.Split(' ') 
相关问题