2014-10-16 42 views
1
Dim amountorhour As String = "A4" 

If amountorhour.Substring(0, 1) = "A" Then 
    amount = amountorhour.Substring(1, amountorhour.Length)--**error comes here** 
    HrsWorked = 0 
Else 
    HrsWorked = amountorhour.Substring(1, amountorhour.Length - 1) 
    amount = 0 

我知道这个错误显示出来,当我询问字符串的长度超出范围但在这种情况下,我没有看到类似的东西请帮助我弄清楚问题如何解决索引和长度必须引用字符串中的位置。参数名称:长度“?

回答

2

第二个参数是从指数的子您指定的长度,所以通过amountorhour.Length作品只有当你传递0作为将返回原始字符串的第一个参数。

看来你要取除第一个字符以外的所有字符,可以使用带有一个参数的the overload

amount = amountorhour.Substring(1) 

这是一样的

amount = amountorhour.Substring(1, amountorhour.Length - 1) 

顺便说一句,你可以使用的

If amountorhour.StartsWith("A") Then 

代替

If amountorhour.Substring(0, 1) = "A" Then 
+0

感谢蒂姆,有一个参数重载方法的工作。 – user8811 2014-10-16 12:29:12

相关问题