开发者

how do you parse a string in vb6?

开发者 https://www.devze.com 2022-12-12 17:52 出处:网络
Some of us unfortunately are still supporting legacy a开发者_StackOverflowpp like VB6. I have forgotten how to parse a string.

Some of us unfortunately are still supporting legacy a开发者_StackOverflowpp 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


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.


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.


The easiest way is to convert the string into an array of bytes and iterate over the byte array (converting each byte to a character).

Dim str As String
Dim bytArray() As Byte
Dim count As Integer

str = "This is a string."
bytArray = str

For count = 0 To UBound(bytArray)
    Debug.Print Chr(bytArray(count))
Next


Don't loop; rather, set a reference to Microsoft VBScript Regular Expressions library and use regular expressions to achieve your 'do something' goal.

0

精彩评论

暂无评论...
验证码 换一张
取 消