Split using NULL

nagare_boshi
01-27-2008, 11:09 PM
hi everyone.

i was wondering if anyone could help me with this problem..

is there a way that i can split a string to an array of characters using NULL as delimeter?

ex.

input:
someString = "abcde"

output:
someArray(0) = 'a'
someArray(1) = 'b'
someArray(2) = 'c'
someArray(3) = 'd'
someArray(4) = 'e'

i'd really appriciate any help on this..

thanks!:)

mkaras
01-27-2008, 11:27 PM
Not directly using Split() and the sample you have shown.

Your sample gives the idea that the splits would always be wanted across a uniform number of characters per split item. This being the case why don't you just try to make a loop to produce the results using Mid$() to pull apart the string value.

dilettante
01-27-2008, 11:39 PM
Hard to say what your purpose is here so it may not help, but you could assign to a dynamic Byte array via StrConv() using the conversion type vbFromUnicode as long as you're dealing with the ANSI character subset of VB's native UTF-16 character encoding:

bytArray = StrConv("abcde", vbFromUnicode)

nagare_boshi
01-28-2008, 12:03 AM
what i'm trying to do here is to 'split' a string into several smaller elements..

Ex.
someArray = Split("abcde", "")
-> this would return the whole string and not an array of strings..

what i want to achieve is to be able to split "abcde" and store it to an array without using any looping structure..

now, using bytArray = StrConv("1111", vbFromUnicode) does that (thanks dilletante ;) )..
however, it returns an array of bytes whose elements are all 49..

ex.
bytArray = StrConv("1111", vbFromUnicode)

output:
bytArray(0) = 49
bytArray(1) = 49
bytArray(2) = 49
bytArray(3) = 49

dilettante
01-28-2008, 12:11 AM
Well a "1" is 49 in decimal. Byte values aren't String values, they are unsigned 8-bit values. That's why I suggested it depends on what you want to accomplish.

nagare_boshi
01-28-2008, 12:16 AM
i want to be able to get the values as they are..

well, if that's the case, can i convert the whole bytArray to character?

is there a way?

mkaras
01-28-2008, 01:22 AM
Just write the loop.

If your string was "a,b,c,d,e,f" such that even Split could work using "," as the delimiter I can assure you that Split() has a loop inside of itself to perform the function.

So why is it so all fired necessary to try to bend a simple problem into something so complicated and obtuse?

nagare_boshi
01-28-2008, 01:43 AM
you simply don't get it... and you're not helping either.

i know how Split() works, and it can't give me the result i want.

what i want is to split a string with NO DELIMITER.

simple. get it?

now, Split() can't do that.

that's why i'm here and asking for HELP.

jantje
01-28-2008, 01:58 AM
He does get it. He's telling you to write a simple loop yourself which can't be hard...

Here's some pseudo code:
Function CharArray(ByRef SomeString As String) As String()

Dim i As Integer
Dim saRet() As String

If Len(SomeString) <> 0 Then
ReDim saRet(Len(SomeString))

For i = 0 To Len(SomeString)
saRet(i) = Mid$(SomeString, i + 1, 1)
Next

CharArray = saRet
End If

End Function


It's up to you to clean it up a bit and make it all nice and funky. There's no native VB function that does what you want, so you'll have to write your own.

DougT
01-28-2008, 03:44 AM
This is so horrible it makes my teeth ache but I think it does what you want

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)

Private Sub Command1_Click()
Dim strChars As String
Dim strSplit() As String
Dim bytArray() As Byte
Dim inti As Integer
bytArray = StrConv("abcd", vbUnicode)
strChars = Space(UBound(bytArray) + 1)
CopyMemory ByVal strChars, bytArray(0), UBound(bytArray) + 1
strChars = Replace(strChars, Chr(0) & Chr(0) & Chr(0), Chr(0))
strSplit = Split(strChars, Chr(0))
'
' strSplit now holds one character per element
' to prove it display in the Immediate window
' and no, I don't know how to do that without a loop
'
For inti = LBound(strSplit) To UBound(strSplit)
Debug.Print strSplit(inti)
Next inti
End Sub

BTW please review our .. especially #5.

nagare_boshi
01-28-2008, 04:12 AM
It worked.

Thanks DougT...

And yeah, I'm aware of guideline #5.

Just got carried away.. Sorry.

Thanks again. :D

mkaras
01-28-2008, 04:46 AM
...
bytArray = StrConv("abcd", vbUnicode)
strChars = Space(UBound(bytArray) + 1)
CopyMemory ByVal strChars, bytArray(0), UBound(bytArray) + 1
strChars = Replace(strChars, Chr(0) & Chr(0) & Chr(0), Chr(0))
strSplit = Split(strChars, Chr(0))
...


Every single line of code in the above uses a looping structure to accomplish what it does!!

nagare_boshi
01-28-2008, 05:31 AM
yeah, but its the one i was looking for..

thanks anyways.. mkaras. :)

mkaras
01-28-2008, 06:04 AM
I ran a timing test on my system here to compare the performance of running the code that was shown in post #9 and post #10.

Post 9 code: Run 10E6 times - Execution Time = 9 seconds
Post 10 code: Run 10E6 times - Execution Time = 31 seconds

(Run times performed inside IDE and verified with three separate iterations)

Test code could be posted if it is desired.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum