Question

Consider:

char [] chararray = txt1.Text;

How we do the same in Visual Basic 6.0?

Was it helpful?

Solution

That depends on what you eventually want to do.

You can, for example, do this in VB6:

Dim b() As Byte
b = Text1.Text

That way the b array will be resized to hold the Unicode data from "string" -- but then each character will be split across two bytes which is probably not what you want. This trick only works with Byte.


You also can do that:

Dim b() As Byte
b = StrConv(Text1.Text, vbFromUnicode)

Each letter will now occupy one byte, but the extended characters will be gone. Only do this if the current system code page contains the required characters.


You can copy the characters manually to an array:

Dim s() As String, i As Long
ReDim s(1 To Len(Text1.Text))

For i = 1 To UBound(s)
  s(i) = Mid$(Text1.Text, i, 1)
Next

Or you can even avoid creating an array at all, becase Mid also serves as an indexer operator that changes a character in place, without copying or allocating anything:

Dim s As String
s = Text1.Text

Mid$(s, 3, 1) = "!"

OTHER TIPS

You can't do the same in VB6, as it doesn't have a character data type.

You can split the string into an array of strings, each containing a single character:

Dim chararray(Len(txt1.Text) - 1) As String
For i = 1 to Len(txt1.Text)
  chararray(i - 1) = Mid(txt1.Text, i, 1)
Next

Edit:

To traverse a string and replace characters, you can simply loop over it's length and use the string function to manipulate it:

' Copy the value of the proeprty to a local variable
Dim text as String = txt1.Text
' Loop over the length of the string
For i = 1 to Len(text)
  ' Check the value of a character
  If Mid(text, i, 1) = " " Then
    ' Replace a character
    Mid(textx, i, 1) = "*"
  End If
Next

VB6 has a String type so this code simply becomes:

Dim x As String
x = Text1.Text

You can manipulate that text in-place and manipulate individual characters using the VB6 string functions.

In the rare cases where you really need an array of the caracter codes, you need to declare a Byte array (VB has no char type), then you can simply assign the string to the array, or use StrConv to handle Unicode code points differently, as shown by @GSerg.

String To Array:

Public Function str2Array(xString As String) As String()
Dim tmpArray() As String
Dim tmpchar As String

' /* For Each Character In The String */
For I = 1 To Len(xString)

    ' /* Retrieve The Character */
    tmpchar = Mid(xString, I, 1)
    ' /* Push It Into The Temporary Array */
    spush tmpArray, tmpchar
Next I

' /* Return The Array To The Calling Procedure */
str2Array = tmpArray
End Function

You can get the UNICODE value of each character of the string in this way:

Dim chararray(1 To Len(txt1.Text)) As Long

For i = 1 To Len(txt1.Text) chararray(i) = ASCW(Mid(Text1.Text, i, 1)) Next

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top