0

I have a string variable as:

p_text = "}{X=45,Y=65}{X=59,Y=65}{X=59,Y=79}{X=45,Y=79}{"

with this code

Dim p_text As String
Dim s1_texts As String()
Dim s2_texts As String()
Dim s3_texts As String()

s1_texts = Split(p_text, "}{")
Dim numi As Integer = 0
Dim numj As Integer = 0
Dim numk As Integer = 0

Do Until numi = s1_texts.Length - 1
    If s1_texts(numi) = "" Then
        numi += 1
    Else
        s2_texts = Split(s1_texts(numi), ",")
        Do Until numj = s2_texts.Length - 1
            If s2_texts(numj) = "" Then
                numj += 1
            Else
                s3_texts = Split(s2_texts(numj), "=")
                Do Until numk = s3_texts.Length - 1
                    TextBox4.Text = TextBox4.Text & s3_texts(numk) & vbCrLf
                    numk += 1
                Loop
                numj += 1
            End If
        Loop
        numi += 1
    End If
Loop

And I want TextBox4 to show:

X

45

Y

65

x

59

Y

65

.......

But I am only getting:

x

x

x

...

Where is the problem?

S.Serpooshan
  • 7,608
  • 4
  • 33
  • 61
nsssayom
  • 364
  • 1
  • 5
  • 21

3 Answers3

1

Or you can just replace all non-alphanumeric characters with new line:

TextBox4.Text = Regex.Replace(p_text, "\W+", vbNewLine).Trim
Slai
  • 22,144
  • 5
  • 45
  • 53
0

(Although not directly addressing the loop problem)

This might help. The Split() function can accept multiple characters. Therefore, you could simply do:

Sub Main()

    ' Original string
    Dim p_text = "}{X=45,Y=65}{X=59,Y=65}{X=59,Y=79}{X=45,Y=79}{"

    ' String array split
    Dim p_text_new = p_text.Split("}"c, "{"c, "="c, ","c)

    ' Iterate through each string in array
    For Each p In p_text_new

        ' Check to ensure you don't print empty spaces
        If Not String.IsNullOrWhiteSpace(p) Then
            Console.WriteLine(p) ' p is the value you want....
        End If

    Next

End Sub
Inisheer
  • 20,376
  • 9
  • 50
  • 82
0

If i want to not change your code but only adjust its bug, this is my answer:

When you use Do Until x = N and you increase the x at end of the loop, the last value of x will not pass the condition you specified at beginning of the loop and so will not be executed.

Your loops have a mistake: your end condition is not specified correctly (....Length - 1 is one less than the last item because). Also the numj and numk counters shall be reset to zero at begining inner loops.

Modify your code as following:

s1_texts = Split(p_text, "}{")
Dim numi As Integer = 0
Dim numj As Integer = 0
Dim numk As Integer = 0

Do Until numi = s1_texts.Length '- 1 => *** removed!
    If s1_texts(numi) = "" Then
        numi += 1
    Else
        s2_texts = Split(s1_texts(numi), ",")
        numj = 0 '*** reset counter to 0
        Do Until numj = s2_texts.Length '- 1 => *** removed!
            If s2_texts(numj) = "" Then
                numj += 1
            Else
                s3_texts = Split(s2_texts(numj), "=")
                numk = 0 '*** reset counter to 0
                Do Until numk = s3_texts.Length '- 1 => *** removed!
                    TextBox4.Text = TextBox4.Text & s3_texts(numk) & vbCrLf
                    numk += 1
                Loop
                numj += 1
            End If
        Loop
        numi += 1
    End If
Loop

and it will produce what you want.

But if you use For-Next loop, it could be more easy:

s1_texts = Split(p_text, "}{")
Dim numi, numj, numk As Integer

For numi = 0 To s1_texts.Length - 1
    If s1_texts(numi) <> "" Then
        s2_texts = Split(s1_texts(numi), ",")
        For numj = 0 To s2_texts.Length - 1
            If s2_texts(numj) <> "" Then
                s3_texts = Split(s2_texts(numj), "=")
                For numk = 0 To s3_texts.Length - 1
                    TextBox4.Text = TextBox4.Text & s3_texts(numk) & vbCrLf
                Next 'numk
            End If
        Next 'numj
    End If
Next 'numi

Note: here we should use ... .Length - 1 for the condition.

S.Serpooshan
  • 7,608
  • 4
  • 33
  • 61