string parsing


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: string parsing

  1. #1
    Join Date
    Dec 2004
    Posts
    14

    string parsing

    Hi all,

    I'm developing an application for a game. It assists in calculating and note management for an online trading game. Now for the notes part I still have a small part of the application unfinished. In the game, when another player for example sells you something, you get a PM (ingame) with a small report for example:

    Mister X delivered some goods to your planet.

    Dynamite:5000, Fuses:12000, Blabla:6000

    You had 5000 dynamite, 12000 fuses and 6000 blabla

    You now have 10000 dynamite, 24000 fuses and 12000 blabla

    I need to break this clipboard string into pieces and search for "dynamite:" and whatever number is listed behind it.

    Now sometimes the report lists Dynamite:5000, but sometimes (don't know why) it adds a space like Dynamite: 5000 - so delimiters are not always accurate.

    I need a search routine wich searches for keywords in this text and the value it has (after the semicolon).

    Can anyone help me out plz?



    Hans

    Holland

  2. #2
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    You can use Replace and Split to turn the list into an array where the first element holds the Item Name the Next element holds the Item count, then item name and then item count, etc.. then just loop through the array with a step of two so that Item(I) holds the name and Item(I+1) holds the count.
    Code:
    Function Itemize(goods As String) As Variant
      Dim it() As String
      Dim buf As String
      
      buf = Replace(goods, " ", "") ' strip out spaces
      buf = Replace(buf, ":", ",")  ' convert colon's to commas for split
      it = Split(buf, ",")          ' spilt item and counts into array
      Itemize = it                  ' return array
    End Function
    
    Sub ItemizeTest()
      Dim I As Long
      Dim test As String
      'Dim Items() As String
      Dim Items As Variant
      Dim Dynamite As Long
      
      
      test = "Dynamite:5000, Fuses:12000, Blabla:6000"
      
      Items = Itemize(test)
      
      For I = LBound(Items) To UBound(Items) - 1 Step 2
        Debug.Print Items(I) & " is " & Items(I + 1)
        If LCase(Items(I)) = "dynamite" Then Dynamite = CLng(Items(I + 1))
      Next I
      'Look for dynamite
      MsgBox "Found " & Dynamite & " of Dynamite!"
      
    End Sub

  3. #3
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Another option (more complex, less code) is to use a regular expression:
    Code:
    Dim test As String
    Dim exp As RegExp
    Dim matches As MatchCollection
    Dim DynamiteValue As Integer
    
    test = "Mister X delivered some goods to your planet. " & vbCrLf & _
        "Dynamite:5000, Fuses:12000, Blabla:6000 " & vbCrLf & _
        "You had 5000 dynamite, 12000 fuses and 6000 blabla " & vbCrLf & _
        "You now have 10000 dynamite, 24000 fuses and 12000 blabla"
        
    Set exp = New RegExp
    exp.Pattern = "Dynamite:\s*(\d+),"
    Set matches = exp.Execute(test)
    DynamiteValue = matches(0).SubMatches(0)
    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!

  4. #4
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    Looks interesting, but from what I can tell it only works in vb.net

  5. #5
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    No, that's VB6 code. You must add a reference to the VBScript Regular Expressions Library.
    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!

Similar Threads

  1. Input string was not in a correct format
    By mdengler in forum ASP.NET
    Replies: 0
    Last Post: 11-26-2002, 03:32 PM
  2. Replies: 1
    Last Post: 06-05-2001, 07:12 AM
  3. Please help me -- urgent -- deadlock error
    By chandra in forum VB Classic
    Replies: 0
    Last Post: 06-22-2000, 08:36 AM
  4. Replies: 0
    Last Post: 06-22-2000, 08:30 AM
  5. Database problems
    By Robert Rieth in forum VB Classic
    Replies: 1
    Last Post: 04-11-2000, 04: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