Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you parse a string in vb6?

Tags:

vb6

Some of us unfortunately are still supporting legacy app like VB6. I have forgotten how to parse a string.

Given a string:

Dim mystring As String = "1234567890"

How do you loop in VB6 through each character and do something like

   for each character in mystring
      debug.print character
   next

In C# i would do something like

 char[] myChars = mystring.ToCharArray();
 foreach (char c in theChars)
 {
    //do something with c
 }

Any ideas?

Thanks a lot

like image 336
jo. Avatar asked May 13 '26 07:05

jo.


2 Answers

You can use the 'Mid' function to get at the individual characters:

Dim i As Integer
For i = 1 To Len(mystring)
    Print Mid$(mystring, i, 1)
Next

Note this is untested.

like image 90
Erik Forbes Avatar answered May 19 '26 05:05

Erik Forbes


There is no possibility to use foreach on strings.

Use

Dim i As Integer

For i = 1 To Len(YourString)
    Result = Mid$(YourString, i, 1)
Next

note that the type of Result is a length-1 string, no char or byte type.

If performance is important, you'll have to convert the string to a bytearray fist (using StrConv) and then loop through it like this.

Dim i As Long
For i = 0 To UBound(Data)
    Result = Data(i) ' Type is Byte '
Next

This is much more efficient.

like image 32
Dario Avatar answered May 19 '26 04:05

Dario



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!