Results 1 to 14 of 14

Thread: use split to split string to char???

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2009
    Posts
    23

    Question 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

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: use split to split string to char???

    vb Code:
    1. s1=split("a/b2,E","")
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Aug 2009
    Posts
    23

    Re: use split to split string to char???

    Quote Originally Posted by Pradeep1210 View Post
    vb Code:
    1. s1=split("a/b2,E","")
    that doesn't work it doesn't split any thing it stays as it is
    s1(0) is "a/b2,E"

  5. #5
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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

  6. #6
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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

  7. #7
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    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

  8. #8
    New Member
    Join Date
    Sep 2009
    Posts
    1

    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.

  9. #9
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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.

  10. #10
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  11. #11
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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.

  12. #12
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.
    W o t . S i g

  13. #13
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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.

  14. #14
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width