|
-
Jan 3rd, 2010, 01:56 PM
#1
Thread Starter
Junior Member
use split to split string to char???
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
-
Jan 3rd, 2010, 02:03 PM
#2
Re: use split to split string to char???
-
Jan 3rd, 2010, 02:12 PM
#3
PowerPoster
Re: use split to split string to char???
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 :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jan 3rd, 2010, 02:27 PM
#4
Thread Starter
Junior Member
Re: use split to split string to char???
 Originally Posted by Pradeep1210
that doesn't work it doesn't split any thing it stays as it is
s1(0) is "a/b2,E"
-
Jan 3rd, 2010, 04:31 PM
#5
Re: use split to split string to char???
smUX has it about right
Code:
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
Edit: actually this might not be what you want, the byte array will contain the character codes not the characters
Last edited by Milk; Jan 3rd, 2010 at 04:51 PM.
Reason: oopsy
W o t . S i g
-
Jan 3rd, 2010, 05:55 PM
#6
Re: use split to split string to char???
how about this
Code:
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
-
Jan 3rd, 2010, 06:26 PM
#7
Re: use split to split string to char???
Vin
If you are truly only trying to capture every character,
then how about something like this:
Code:
txt = "a/b2,E"
ln = Len(txt)
Dim s1(ln)
For ii = 1 to ln
sl(ii - 1) = Mid(txt, ii, 1)
Next ii
Spoo
-
Jan 3rd, 2010, 06:44 PM
#8
New Member
Re: use split to split string to char???
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.
-
Jan 3rd, 2010, 07:58 PM
#9
PowerPoster
Re: use split to split string to char???
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 :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jan 3rd, 2010, 10:35 PM
#10
Re: use split to split string to char???
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.
-
Jan 4th, 2010, 07:10 AM
#11
PowerPoster
Re: use split to split string to char???
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
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jan 4th, 2010, 07:42 AM
#12
Re: use split to split string to char???
smUX, although what we posted is fast way to get at those character codes it's NOT what the OP asked for.
-
Jan 4th, 2010, 08:11 AM
#13
PowerPoster
Re: use split to split string to char???
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
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jan 4th, 2010, 08:32 AM
#14
Re: use split to split string to char???
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|