Splitting text into separate characters

Neon612
04-22-2006, 09:39 PM
if I have a text string (for example "Hello World")
how can i separate out the characters instead of just the words to variables
(I want to get something that looks like this:

"HEllo World"
text(0)=H
text(1)=e
text(2)=l
......

and so on)


How would I go about doing this?

CarlosR
04-22-2006, 09:52 PM
try this

'//Setup Variables
Dim n As Integer
Dim tempStr As String '= "Hello World"

'//Loop through each character in tempStr, and store it in temp(n)
For n = 0 To Len(tempStr) - 1
temp(n) = Mid(tempStr, n, 1)
Next n



just replace tempstring with whatever variable you use to store "Hello World", or just remove the ' in the declaration.

Neon612
04-22-2006, 10:03 PM
I've tried it and the temp(n) part keeps on giving me an error (Sub or Function not defined)

Also Something I forgot to mention is that I need to get the text from a textbox and put the letters out to another textbox. Which I'm not so sure but I may be able to figure out for myself.

CarlosR
04-22-2006, 10:14 PM
Oh yea, your going to have to put the code inside of a sub/function...doesn't do any good in the declaration section.

As for the rest, are you trying to just copy text from one textbox to another? If so, you can just do something like TextBox2.Text = TextBox1.Text
For that, just place a button on the form, name it what you want, double click the button to bring up its "Click Event" and enter the following in that function

TextBox2.Text = TextBox1.Text

Neon612
04-22-2006, 10:18 PM
The second textbox is what the characters will go into once i can get them separated

And what do you mean by sub/function?
I've got the code you gave me under a command button click command.

Diurnal
04-22-2006, 11:26 PM
In reality, strings are byte arrays. You can loop through the array and push it to a string array or just use it like it is:

Dim b() As Byte
Dim i As Long

'Assign the string to the array.
b() = "Hello World"

'Print the string.
Debug.Print b()

'Print each character in the string (Unicode!).
For i = 0 To UBound(b()) Step 2
Debug.Print ChrW$(b(i))
Next i

Neon612
04-23-2006, 11:37 AM
So far so good.
But the letters don't all go into a textbox only the last one does.
How can i make it so they all do with a comma separating them?

(I did change the Debug.Print to the name of the textbox)

With the Debug.print the letters go into the immidiate window but no where on the form, but that did what i wanted it to do.

wakjah
04-23-2006, 12:05 PM
To start with, since you're using standard TextBoxes it can be safely assumed that you do not need unicode, and thus the following code should be used to assign the byte array in the first place:


Dim b() As Byte

b = StrConv(txtWhatever.Text, vbFromUnicode)


This will put the string into the array without the nulls inbetween each character.

As for putting the characters, comma delimited, into another textbox, the following line will do this:


txtWhatever2.Text = Join(b, ",")

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum