Split word into letters

kenneth.campos

Registered User.
Local time
Today, 06:13
Joined
Jan 2, 2010
Messages
11
Can I use split like this?

myVar = Split(whatever)(0)
myVar1 = Split(whatever)(1)
myVar2 = Split(whatever)(2)
myVar3 = Split(whatever)(3)

resulting in

myVar = w, myVar1 = h, myVar2 = a, myVar3 =t, etc...
 
Yes you can because the Split() function returns an array. Any function, including user written functions, that returns an array can also be sub-scripted.
 
Except the Split() function needs a delimiter or defaults to a space " ". Getting it to break up a string into its letters would require the creation on a function (UDF).
 
Ah yes, but that point was covered by the generic ‘whatever’. :D

Code:
Sub TheDevilIsInTheDetail()
    Dim myVar, myVar1, myVar2, myVar3   [color=green]' as whatever[/color]
    Dim whatever                        [color=green]' as whatever[/color]
    
    On Error GoTo WhereEver
    
    whatever = "w,h,a,t"
    
    myVar = Split(whatever, ",")(0)
    myVar1 = Split(whatever, ",")(1)
    myVar2 = Split(whatever, ",")(2)
    myVar3 = Split(whatever, ",")(3)
    
    MsgBox myVar    [color=green]' << w[/color]
    MsgBox myVar1   [color=green]' << h[/color]
    MsgBox myVar2   [color=green]' << a[/color]
    MsgBox myVar3   [color=green]' << t[/color]
    
AtThisStageWhoCares:
    Exit Sub

WhereEver:
    MsgBox "Error in Sub TheDevilIsInTheDetail - " & Err.Number
    Resume AtThisStageWhoCares
    
End Sub
 
Last edited:
So how do you get the commas between each letter of the word, smarty pants? :p And don't say with the keyboard because that would be cheating! ;)
 
Darwin.

Whatever’s naturally grow with commas separating their internal parts.
The most common variety is the CSV Whatever; however, other mutations have spaces and even pipes.
The pipe variety is often found in Ireland and Scotland.
 
Funny, you don't seem to write with an accent! :confused: :eek:
 
If you can na hear the accent, tis because of the pipes laddie. :D
 
I thought this would work:

Code:
Dim myVar as Variant
oVar = VariedLengthString
loVar = Len(oVar)
For i = 1 to loVar
    toVar = Left(oVar, i)
     myVar(i) = toVar
Next i

But i get
Run-time Error '13':
Type Mismatch
 
Seing as nobody else has asked I will, what is the object of the exercise?

David
 
I am creating a database to store computer information. Using the computer name from active directory I pull IP, MAC, Make/Model, and Serial Number over the network. The basic database works, but I’m trying to make it more :cool:. This varied length string comes from us having different make/models of computers so I can't predict which will pop up. If I can get these strings to come up with a variable for each letter, I can then manipulate them however I want, such as place them in a different orders before showing the actual string.
 
This function will take in one word and return the word with a comma delimited between each letter

Code:
Public Function SplitWordOrWords(AnyPhrase As String) As String

'************************************************
'If the string you are passing to the function may contain spaces
'Then use the following line to remove them.
'************************************************

AnyPhrase = Replace(AnyPhrase," " , "")

Dim I As Integer
Dim Result As String
For I = 1 To Len(AnyPhrase)
     Result = Result &  "," & Mid(AnyPhrase,I,1)
Next I

SplitWordOrWords = Result

End Function

Then if you use the Split(SplitWordOrWords("Test Phrase"),",")

it should return an array with each letter in the array

David
 
David,
Did you test your code? I believe it starts the return string with a comma.
 
btw OP,

now that I think about it, you can do this with my array shuffler .

use a static array, LIKE THIS:
PHP:
option base 1

Dim i As Integer
      Dim strVAR As String

strVAR = Inputbox("Please enter your word")

        Dim var(Len(strVAR)) As Variant

  For i = LBound(var) To UBound(var)
    
    var(i) = Mid(strVAR, i, 1)
    Debug.Print var(i) //DO WHATEVER YOU WANT WITH IT HERE
  
  Next i
cool! :D
 
Last edited:
RG

Yes it does, and it does intentionally, what I forgot to include was the following line

Code:
Result = Mid(Result,2)

This strips of the leading comma. A lot neater than trying to detect the length of the string and using the Left() command.

David
 
why would you need to split a string into an array

simply len(mystring) determines the length

and mid(mystring,character,1) returns the character at a desired position

mid must surely be implemented as some sort of array vector anyway, so actually transposing a string into an array will probably not be any more efficient
 

Users who are viewing this thread

Back
Top Bottom