example:
s1=split("a/b2,E",
how can i split "a/b2,E"
to
s1(0)='a'
s1(1)='/'
s1(2)='b'
s1(3)='2'
s1(4)=','
s1(5)='E'
thanks is advanced
Printable View
example:
s1=split("a/b2,E",
how can i split "a/b2,E"
to
s1(0)='a'
s1(1)='/'
s1(2)='b'
s1(3)='2'
s1(4)=','
s1(5)='E'
thanks is advanced
vb Code:
s1=split("a/b2,E","")
Another way would be to turn it into a byte array, simply dimension a variable as byte ("byt() as byte" for instance) and then do byt() = "a/b2,E" and byt(0) to byt(5) would contain the byte values. Can't remember if you have to strconv or not (I tend to check whenever I use this, and modify accordingly) fromunicode or unicode, but you can work that out too :-)
smUX has it about right
Edit: actually this might not be what you want, the byte array will contain the character codes not the charactersCode:Dim Chars() as byte
Chars = "123456" 'this will produce an array of 12 elements, every odd index will be 0
Chars = StrConv("123456",VbFromUnicode) 'this will produce an array of 6 elements
how about thisCode:Private Sub Command1_Click()
Dim strString As String
Dim strChars() As String
Dim i As Integer
strString = "a/b2,E"
strString = StrConv(strString, vbUnicode)
strChars = Split(strString, Chr(0))
For i = 0 To UBound(strChars) - 1
Debug.Print strChars(i)
Next i
End Sub
Vin
If you are truly only trying to capture every character,
then how about something like this:
SpooCode:txt = "a/b2,E"
ln = Len(txt)
Dim s1(ln)
For ii = 1 to ln
sl(ii - 1) = Mid(txt, ii, 1)
Next ii
Why not:
Code:For each chrTest as Char in strText
Debug.Print(chrTest) 'or whatever you want to do
Next
Is there something I am missing?
Okay, my bad, I was looking up at the VB.NET forum and clicked on this forum. Nevermind.
I think mine and MarkT's are the best options...mine for simplicity if you don't mind the data being CHR values rather than string data and MarkT's if they have to be strings...his is best overall as it's exactly what's been asked for and it's the fastest way to do it and it could be merged into a one-line conversion :-)
MarkT's code loooks very "professional" but it's better to redim (with preserve) to remove the last element instead of using UBound(strChars) - 1
I think Spoo's code is the fastest one (about twice faster than using StrConv and Split) as it uses only basic functions Len() and Mid(). It also works with "pre-Split" versions of VB as well.
Although mine using the byte array will be faster if you can work with chr values rather than string data...all depends on the application for the method, I'd say...mine is pretty much one command and it's available (AFAIK) in all versions of VB that are discussed here
smUX, although what we posted is fast way to get at those character codes it's NOT what the OP asked for.
BUT the OP was general in their request, hence my comment on the fact that *if* they can work with CHR values ours would be best...it's possible that with minor modification of their code it would make a huge difference to the speed :-)
Well, when I say general, I mean they didn't specifically state it had to be strings and didn't give any more info...they also haven't returned to confirm if our method would work for them, and that's what I'm waiting for :-P
A minor error with Spoo's code: Dim s1(ln) is only valid if ln is a constant.
Proper Functions in two different ways:
Code:Function SplitChars(ByVal sText As String) As String()
If Len(sText) Then
sText = StrConv(sText, vbUnicode)
sText = Left$(sText, Len(sText) - 1) '-- remove trailing chr(0)
SplitChars = Split(sText, Chr$(0))
Else
SplitChars = Split("") '-- Lbound = 0, Ubound = -1
End If
End Function
Code:Function SplitChars2(sText As String) As String()
If Len(sText) Then
Dim i As Long
ReDim ch(Len(sText) - 1) As String
For i = 0 To UBound(ch)
ch(i) = Mid$(sText, i + 1, 1)
Next
SplitChars2 = ch
Else
SplitChars2 = Split("") '-- Lbound = 0, Ubound = -1
End If
End Function