tags:

views:

46

answers:

4
char [] chararray = txt1.Text;

how we do the same in vb 6.0

+3  A: 

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) = "!"
GSerg
@GSerg: this all are very helpful for me , actully i am just a beginner in vb6 ... what i want to do as- "i am going" is a string and i want to replace all gaps with * using char array. thank you
ashish
@ashsih String manipulation in VB6 is usually done directly with strings, not with character arrays. Have you looked at the Replace function? See http://msdn.microsoft.com/en-us/library/aa241892(v=VS.60).aspx It replaces all occurrences of a substring with another string. What exactly do you mean by "gaps"? Spaces? Try `str = Replace(str, " ", "*")`
MarkJ
@GSerg: i have already try this , but my basic quesstion is that how we will traverse all characters one by one using string/char array
ashish
@ashish By using a For loop, such as one used in the third example.
GSerg
A: 

String To Array

M.H
+1  A: 

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.

Konrad Rudolph
+2  A: 

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
Guffa
@Konrad Rudolph: Neither of the other methods do the same thing, so the code is of course not redundant. Please don't badmounth other answers like that without reason.
Guffa
@Guffa: I don’t see it – what does your code differently from `b = StrConv(Text1.Text, vbFromUnicode)` (except of course for the extra byte value in the array)?
Konrad Rudolph
@Konrad Rudolph: It produces an array of strings, which I clearly pointed out in the answer, not an array of bytes.
Guffa
@Guffa: Wow, carefully reading really helps. I completely overlooked that.
Konrad Rudolph