Split function

  • Thread starter Thread starter Wavequation
  • Start date Start date
W

Wavequation

I am trying to take a string consisting of contiguous charaters, i.e with no
spaces or other delimiters, only letters and numbers, and put each individual
character into an array. I tried using split(), with "" as a delimiter, e.g.
split("test", ""), but it doesn't seem to work. Is there any way to do it?
 
Wavequation said:
I am trying to take a string consisting of contiguous charaters, i.e with
no
spaces or other delimiters, only letters and numbers, and put each
individual
character into an array. I tried using split(), with "" as a delimiter,
e.g.
split("test", ""), but it doesn't seem to work. Is there any way to do
it?

Untested Air code:

Dim i As Long
Dim MyString As String, tmpLen As Long

MyString = "qwertyuiop"

tmpLen = Len(MyString)
ReDim MyArray(0 To tmpLen - 1)

For i = 0 To tmpLen
MyArray(i) = Mid(MyString, i + 1)
Next
 
Close, but you need to add one more argrument to the Mid function.

Dim i As Long
Dim MyString As String, tmpLen As Long
Dim MyArray() as String
MyString = "qwertyuiop"

tmpLen = Len(MyString)
ReDim MyArray(0 To tmpLen - 1)

For i = 0 To tmpLen
MyArray(i) = Mid(MyString, i + 1,1)
Next

Public Sub fQtest(strIn)
Dim i As Long
Dim MyString As String, tmpLen As Long
Dim MyArray() As String

If Len(strIn & "") > 0 Then
MyString = strIn
tmpLen = Len(MyString) - 1

ReDim MyArray(tmpLen)

For i = 0 To tmpLen
MyArray(i) = Mid(MyString, i + 1, 1)
Next
End If

End Sub



John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
that's correct John, I actually ended up writing the code while I was waiting
for a reply, and found the Mid() function in the literature and used it. You
must specify 1 character as the last argument of mid().
 
Wavequation said:
I am trying to take a string consisting of contiguous charaters, i.e with no
spaces or other delimiters, only letters and numbers, and put each individual
character into an array. I tried using split(), with "" as a delimiter, e.g.
split("test", ""), but it doesn't seem to work.

Here's a simple example:

Dim myarray(1000) As String
For k = 1 To Len(thestring)
myarray(k) = Mid(thestrimg,k,1)
Next k
 
Marshall Barton said:
Here's a simple example:

Dim myarray(1000) As String
For k = 1 To Len(thestring)
myarray(k) = Mid(thestrimg,k,1)
Next k

Followed by a

ReDim Preserve myarray(0 To Len(thestring) - 1)

(presumably)
 
Was trying to figure out, what is the purpose of adding the & "" when trying
to determine if the length of the string is greater than zero? Some thoughts
came to mind, is it to ensure that a number is returned even if the object
passed into the subroutine (strIn) is actually a string? Shouldn't the
subroutine require it to be a string (i.e. Public Sub fQtest(strIn as
String)) or does that "limit" the capability of the subroutine to work on
other data types that come in?
 
Adding the "" to whatever is passed in ensures that you can test the length.

NULL & "" will have a length of zero
"" & "" will have a length of zero

You could test other ways, but this one is quick and simple and handles
numbers, dates, strings, and nulls

Len(9) will error, but Len(9 & "") will return 1

So if we know that the argument is going to be a string or null, we could do:

If IsNull(strin)=False Then
If Len(strin)>0 Then
'Do stuff
End If
End If

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
So it effectively converts the passed information to a string without having
to use a CStr(datavariable) command within the len command. I think
CStr(datavariable) where datavariable is NULL may throw an error, though I
haven't looked up the effect of that, and therefore like you said using & ""
would "shorten" the code though could lead to other problems if Access
doesn't handle the passed information correctly. Convenient trick I must say
though.
 
Back
Top