Results 1 to 8 of 8

Thread: Split string to array [SOLVED]

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    38

    Split string to array [SOLVED]

    How can i turn this string into a array??
    "{something with space} oneword next item {and so on}"

    Everything inside {} shall be in the same value.

    It shall be like:
    Array(0) shall be "something with space"
    Array(1) shall be "oneword"
    Array(2) shall be "next"
    Array(3) shall be "item"
    Array(4) shall be "and so on"

    I hope you understand how i want it.


    /Rickard
    Last edited by greyhound; Feb 14th, 2003 at 03:30 PM.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    well the way you want to do it.. you would have to do extra parsing using some of the following functions

    Split()
    Mid()
    Left()
    Right()
    InStr()

    Split is a function that will create an array from a string.. but you can only specify one thing for it to split on... so you could split on spaces.. but then it wouldn't keep words in the {} together...

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    There are a couple of ways that come to mind.... one is fairly simple, the other uses brute force.
    The brute force method: Use a loop & temp variables to search for { & } and extract things inbetween....

    The simpler way: Use the Replace function to change all { &} to something else-- like a pipe "|" or something not likely to appear in the text. Then once replaced, use the Split function to split the text into an array.

    VB Code:
    1. Dim strArrayData() As String
    2. Dim strData As String
    3.  
    4.  
    5. strArrayData = Split(Replace(Replace(strData,"}","|"), "{","|"), "|")
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    Originally posted by techgnome

    VB Code:
    1. Dim strArrayData() As String
    2. Dim strData As String
    3.  
    4.  
    5. strArrayData = Split(Replace(Replace(strData,"}","|"), "{","|"), "|")
    will that account for getting the words not in {}??? i don't think it would.. but i didn't run it

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    I think Kleinma has it right. You'll have to extract the values in {} somehow first. I can think of other ways, but they're not necessarily simpler.
    You could split based on spaces, then go back & concatenate or join array elements between { and }. Or, if possible, insert a different character, non-printing if need be, between the elements when creating the original file, and split on that. This would make it easy, if it doesn't mess with some other part of your code.

  6. #6

    Thread Starter
    Registered User
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    38
    Well i got a for working now... i had a BIG bug in it before thats why i never got it to work

    Code:
    Dim strArrayData(0 To 4) As String
    Dim strData As String
    Dim tmpChr As String
    Dim Same As Boolean
    Dim Value As Integer
    Dim i As Integer
    strData = "{something with space} oneword next item {and so on}"
    For i = 1 To Len(strData)
        tmpChr = Mid(strData, i, 1)
        If tmpChr = " " Then
            If Same Then
                strArrayData(Value) = strArrayData(Value) + tmpChr
            Else
                Value = Value + 1
            End If
        ElseIf tmpChr = "{" Then
            Same = True
        ElseIf tmpChr = "}" Then
            Same = False
        Else
            strArrayData(Value) = strArrayData(Value) + tmpChr
        End If
    Next i

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Uh, where do you initially set the value of Same? You test for it before you set it's value. I don't know if booleans default to a value, but you can't rely on that value with different test data sets.
    What if your string is "Foo {something with space} oneword next item {and so on}"? Where's your initial read for If Same then?
    I haven't run this, maybe it would work, but it looks wrong in theory. Test your program with both good and bad data. Don't rely on default values.

  8. #8

    Thread Starter
    Registered User
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    38
    It works as it is now, but your right that it is a good thing to set the values to what i want them to be before it starts.

    "The default value of Boolean is False." says the help files.

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