Parse string with delimiters


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Parse string with delimiters

  1. #1
    Join Date
    Dec 2004
    Posts
    14

    Parse string with delimiters

    Hello programmers out there,

    I have a question. I'm writing an application which allows creating maprotation for the a game. This is currently done by manually editing a config file, leaving room for errors. I have completed the application, but there is a bug in it.

    Take the following string for example.

    +++++++++++++++++++++++++++++++++++

    "gametype dm map hunt gametype dm map SecondComing"

    +++++++++++++++++++++++++++++++++++

    My app reads this string from the config file and parses it with a routine I found on the web. This routine allows you to specify a string and a delimiter and then breaks down the string in pieces whenever the delimiter is found and adds it to a listbox.

    Now if I take the space as the delimiter, the result for the following string would be (quotes are parsed out):

    gametype

    dm

    map

    hunt

    gametype

    dm

    map

    SecondComing

    I would then filter out the garbage from the listbox like "gametype" and "map". All that remains is a proper list:

    dm

    hunt

    dm

    SecondComing

    This works fine when working with single words, but whenever a name has a space in it, it is considered as a space, thus is it parsed.

    For example (same string but with space in SECOND COMING):

    +++++++++++++++++++++++++++++++++++

    "gametype dm map hunt gametype dm map SecondComing"

    +++++++++++++++++++++++++++++++++++

    gametype

    dm

    map

    hunt

    gametype

    dm

    map

    Second

    Coming

    See the problem? Secondcoming is now split. Since this is a dynamic process and there are many cases where the name gets stripped while it shouldn't, this is a major bug. One I can't seem to fix.

    What I need is a snippet that will parse a string (no matter how long) and because it always is the same format it must return the value between GAMETYPE and MAP and the value between MAP and GAMETYPE.

    Lets say the value between GAMETYPE and MAP is assigned to list1 and the value between MAP and GAMETYPE to list2. In my case it would return:

    LIST1.list(0) = DM

    LIST1.list(1) = DM

    LIST2.list(0) = HUNT

    LIST2.list(1) = SECONDCOMING (even when it is SECOND COMING)

    All other info from the string can be discarded. Now I can link the two listboxes and pass back to the app that the gametype (list1.list(0)) is DM for the map HUNT (list2.list(0)) etc.

    I hope this makes any sense. Please note that I gave an example here for two maps (hunt and secondcoming) but that in real config files it may be up to 50 maps in the string. The layout however is the same: the gametype alwas stands between gametype and map and the mapname always stands between map and gametype.

    Thank you so much for helping me.

    Hans

    Holland

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    The simplest solution is to not allow spaces within a name. Instead, write your config file like this:

    gametype dm map hunt gametype dm map Second_Coming

    Split it on spaces as you currently are, then use the Replace function to replace the underscore character with a space or a null string as desired when assigning the string to the listbox.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Jan 2005
    Location
    Cedar Hill, MO, USA
    Posts
    13
    What I would do, in your case, is something like this:

    Assuming you are using VB.Net, if you make a form with two listboxes, one called "lstMap" and the other called "lstGameType". then create a button and in it's click event, copy and paste the following code. I tested it with different lengths and amout of spaces and it seemed to work o.k.


    Dim MyFile As New OpenFileDialog
    Dim result As DialogResult
    Dim strInput As String
    Dim intloop As Integer
    Dim buffer() As String
    Dim strTemp As String
    result = MyFile.ShowDialog
    If result = DialogResult.OK Then
    FileOpen(1, MyFile.FileName, OpenMode.Input)
    Do Until EOF(1)
    Input(1, strInput)
    buffer = strInput.Split(" ")
    For intloop = 0 To buffer.GetUpperBound(0)
    If buffer(intloop).ToLower = "gametype" Then
    Do Until buffer(intloop + 1).ToLower = "map"
    strTemp += buffer(intloop + 1) & " "
    intloop += 1
    Loop
    lstGameType.Items.Add(strTemp)
    strTemp = ""
    ElseIf buffer(intloop).ToLower = "map" Then
    Try
    Do Until buffer(intloop + 1).ToLower = "gametype"
    strTemp += buffer(intloop + 1) & " "
    intloop += 1
    Loop
    Catch
    End Try
    lstMap.Items.Add(strTemp)
    strTemp = ""
    End If
    Next
    Loop
    End If

    -Ray Phillips

Similar Threads

  1. Input string was not in a correct format
    By mdengler in forum ASP.NET
    Replies: 0
    Last Post: 11-26-2002, 02:32 PM
  2. Replies: 1
    Last Post: 06-05-2001, 06:12 AM
  3. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 2
    Last Post: 08-11-2000, 12:11 PM
  4. Please help me -- urgent -- deadlock error
    By chandra in forum VB Classic
    Replies: 0
    Last Post: 06-22-2000, 07:36 AM
  5. Database problems
    By Robert Rieth in forum VB Classic
    Replies: 1
    Last Post: 04-11-2000, 03:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center