Link to home
Start Free TrialLog in
Avatar of pmaxon
pmaxon

asked on

split

How can I split a word up into seperate individual characters?

Key words here are "word" and "characters". Clinton would be:
c
l
i
n
t
o
n
Avatar of Ruchi
Ruchi

try ssomething like this
Private Sub Form_Load()
  Dim TestString
  Dim A() As String
  Dim I As Integer

  TestString = "William Jefferson Clinton"
  A = Split(TestString)

  For I = LBound(A) To UBound(A)
    MsgBox A(I)
  Next
  End
End Sub
Ruchi:
"Try something like this".. <---- That's a definitive answer?.. Shame on you for locking this question down!!!
Now, lets just suppose the questioner does not have VB6.. and ergo NO split command.. what then? Don't me get wrong.. your answer is deserved and accurate in every way.. I just think it belongs as a comment.. and its merit left to the judgement of the questioner.. <smile>.
----------------------
A solution both VB5 / VB6 compatable.

<----- Code Begin ----->

Dim strInput as String
strInput = "This is a test"

Dim strChars() as String
Redim strChars(Len(strInput))

Dim lngIndex as Long
For lngIndex = 1 to Len(strInput)
   strChars(lngIndex - 1) = Mid$(strInput, lngIndex, 1)
Next lngIndex

<----- Code End ----->

Tada..

OK OK OK. Thanks, WSH2



If you have VB6 then you can use Split function

Private Sub Form_Load()
     Dim strString As String
    Dim arrWords As Variant
    Dim i As Integer

    strString = "FirstName MName LastName"
    arrWords = Split(strString, " ")
    For i = 0 To UBound(arrWords)
        MsgBox arrWords(i)
    Next

End Sub




If you have VB5 and earlier versions:


Private Sub Form_Load()
   Dim strString As String
    Dim arrWords() As String
    Dim i As Integer
     
    strString = "FirstName MName LastName"
    Do Until strString = "" 
        ReDim Preserve arrWords(i)
        If InStr(strString, " ") > 0 Then
            arrWords(i) = Trim(Left(strString, InStr(strString, " ") - 1))
            strString = Mid(strString, InStr(strString, " ") + 1)
        Else
            arrWords(i) = Trim(strString)
            strString = "" 
        End If
        i = i + 1
    Loop
     
    For i = 0 To UBound(arrWords)
        MsgBox arrWords(i)
    Next
     
End Sub
Ruchi:
<tipping hat and bowing>.. and Thank YOU as well.. <smile>.

--------------------------------------
pmaxon writes: "How can I split a word up into seperate individual characters?"

Ruchi, questioner is asking how to parse the LETTERS in a word.. not the words themselves.. <smile>.

And a suggestion that I hope you may find useful.. with string variables and string functions such as Instr and Mid it is much more efficient to use the Instr$ and Mid$ forms of the functions (assuming you are NOT using Katanese or some other highly extended characterset.. which fortunately, most of us do not). WITHOUT the $ sign suffix, VB uses two bytes per character, WITH the $ suffix, VB only uses one.. which, as indicated earlier, is absolutely fine on almost all Western (civilization) style computers.. <smile>

Anyhow.. once again, Thank YOU very much for your comments.. I for one, am most appreciative of your thoughts.. keep them coming.. <smile>
Avatar of pmaxon

ASKER

Edited text of question.
Avatar of pmaxon

ASKER

Edited text of question.
Avatar of pmaxon

ASKER

Edited text of question.
ASKER CERTIFIED SOLUTION
Avatar of Ruchi
Ruchi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can use debug.print instead of MsgBox. You will see
c
l
i
n
t
o
n
in the immediate window.
Avatar of pmaxon

ASKER

Adjusted points from 50 to 75
Thank you very much, Pmaxon, for the points! I am glad that I could be of any help to you! Again, thanks.
PSsssst.... a TIP.. test for Zero length strings before running, k?.. <smile> and a <wink>.

Avatar of pmaxon

ASKER

Very good idea wsh2... And thank YOU Ruchi for the help!