String.Split的IndexOutOfRange异常

时间:2014-05-14 19:13:05

标签: vb.net

获取此代码的错误,该代码是接收消息的函数的一部分,它接受查询字符串值并将它们拆分或至少应该是现在它给了我一个超出范围的索引,试图将值赋给paramValue为string=params(1).ToString,我不确定为什么?

Dim QueryString As String() = context.Split("&")
Dim QuerySet As String
Dim Params As String() = Nothing

For Each QuerySet In QueryString
    Params = QuerySet.Split("=")
    Dim ParamKey As String = Params(0).ToString.ToLower
    Dim ParamValue As String = Params(1).ToString

2 个答案:

答案 0 :(得分:0)

不是答案,但不适合作为评论:

For Each QuerySet As String() In context.Split("&"c)
    Dim Params As String() = QuerySet.Split("="c)

Next 

答案 1 :(得分:0)

在代码中添加一些错误检查:

Dim QueryString As String() = context.Split("&"c)
Dim QuerySet As String
Dim Params As String() = Nothing

For Each QuerySet In QueryString
  If QuerySet.Contains("=") Then 'procede
    Params = QuerySet.Split("="c)
    Dim ParamKey As String = Params(0).ToLower
    Dim ParamValue As String = Params(1)
    'use variables
  End If
Next