0

我有一个文本框(DropDownList1),其中包含以下格式的 46 个字符的字符串:

(字符串 1,字符串 2,字符串 3)

我想以这种方式获取不带逗号的字符串值:

一个=字符串1
b=字符串2
c=字符串3

所以我使用了下面的代码:

Dim a As String
Dim b As String
Dim c As String
Dim x As Integer = InStr(1, DropDownList1.Text, ",", CompareMethod.Text) + 1
Dim y As Integer = InStr(InStr(1, DropDownList1.Text, ",", CompareMethod.Text) + 1, DropDownList1.Text, ",") - 1
Dim z As Integer = Len(DropDownList1.Text)
a = Mid(DropDownList1.Text, 1, InStr(1, DropDownList1.Text, ",", CompareMethod.Text) - 1)
b = Mid(DropDownList1.Text, x, y) _
   'InStr(1, DropDownList1.Text, ",", CompareMethod.Text) + 1, _
   'InStr(InStr(1, DropDownList1.Text, ",", CompareMethod.Text) + 1, DropDownList1.Text, ",") - 1)
c = Mid(DropDownList1.Text, _
    InStr(InStr(1, DropDownList1.Text, ",", CompareMethod.Text) + 1, DropDownList1.Text, ",") + 1, _
    Len(DropDownList1.Text))

但是,当我调试它时:

x=18(我使用的字符串是正确的)
y=42(也正确)
z=46(正确)
a=string1(是的!)
c=string3(再次是的!)

和 b=string2,string3 ----->这里发生了什么?

你能告诉我的代码有什么问题吗?我只是不明白

4

4 回答 4

3

在字符串上使用 Split() 函数,将其应用于某个字符串数组变量,然后根据需要将值分配给变量。

于 2013-07-24T20:03:14.770 回答
3

假设 x、y 和 z 仅用于调试,并且您真正关心的是 a、b 和 c,并且字符串的重要部分中没有逗号或括号:

Dim values = DropDownList1.Text.Replace("(","").Replace(")","").Split(","c)
Dim a as String = values(0)
Dim b As String = values(1)
Dim c As String = values(2)
于 2013-07-24T20:08:34.763 回答
1

如果实际上您使用的是 VB.NET,则可以使用Split功能。

Dim text As String = "a,b,c"
Dim parts As String() = text.Split(CChar(","))
Dim a As String = parts(0)
Dim b As String = parts(1)
Dim c As String = parts(2)
于 2013-07-24T20:07:16.873 回答
0

试试这个...

Private Sub ParseMyString()
    Dim TargetString() As String = Split("string1,string2,string3", ",")
    Dim Count As Integer = 0
    Dim Result As String
    Const ASC_OFFSET As Integer = 97

    Result = ""
    Do Until Count > UBound(TargetString)
        Result = Result & Chr(ASC_OFFSET + Count) & "=" & TargetString(Count) & vbCrLf
        Count = Count + 1
    Loop
    MsgBox(Result)
End Sub
于 2013-07-24T20:33:47.040 回答