0

次の形式の 46 文字の文字列を含むテキスト ボックス (DropDownList1) があります。

(文字列1、文字列2、文字列3)

このように、コンマなしで文字列値を取得したい:

a=文字列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

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 に答える