Results 1 to 8 of 8

Thread: [RESOLVED] String parsing

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    294

    Resolved [RESOLVED] String parsing

    Well, I'm only a few months into VB so I suck with parsing stuff. Used to PHP's preg_match(). :P

    Anyway I have loaded the following string from a file:
    Code:
    config
    {
        Key1 "Value1";
        Key2 "Value2";
        Key3
        {
            "Value3";
            "Value4";
        };
    };
    And I want to load it into some soft of 2d array.
    arr("Key1") = "Value1"
    arr("Key2") = "Value2"
    arr("Key3")(0) = "Value3"
    arr("Key3")(1) = "Value4"
    Ideas? Other then looping though string char at a time. lol

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: String parsing

    Can you change the format of the file or is that fixed?

  3. #3
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: String parsing

    This will parse out the values for you. If there will be more than one instance of a sub-key, you can split the string on "{" first to get a grouping of the subkeys.
    VB Code:
    1. Dim sFirst() As String, sSecond() As String, iCount As Integer, sTemp As String
    2.  
    3. sFirst = Split(sString, Chr$(34) & ";")     'where sString is your input.
    4.  
    5. For iCount = 0 To UBound(sFirst) - 1
    6.     sSecond = Split(sFirst(iCount), Chr$(34))
    7.     sTemp = sSecond(UBound(sSecond))
    8.     Debug.Print sTemp
    9. Next iCount

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    294

    Re: String parsing

    Pino, yes.

    Comintern, so im stuck playing with the string functions eh

  5. #5
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: String parsing

    Quote Originally Posted by frozen
    Pino, yes.

    Comintern, so im stuck playing with the string functions eh
    Well, you could use regular expression, but string functions are pretty easy on this one. Check out this link if you want to get started on the RegExp object. I've just personally never used it.

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: String parsing

    That's actually quite complex, to do a proper parse of that would require some recursion I think. Either that or I'm just VERY rusty with VB.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    294

    Re: String parsing

    Bump.

  8. #8
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: String parsing

    Quote Originally Posted by frozen
    Bump.
    I Googled RegExp object model and came up with this. It should get you started. You can also check this link. The Like operator might also be of use.

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