Results 1 to 5 of 5

Thread: Spliting a string

  1. #1
    Frenzied Member
    Join Date
    Sep 08
    Posts
    1,028

    Spliting a string

    Here is an example of a string that I am wishing to use:

    tourney ffa team ctf clan arena
    I am wanting to split it into an array.

    I am currently using this code:

    Code:
    stringCurrentGameTypes = Split(stringTypeList, " ")
    This works. I am wanting to make an exception to certain strings. "clan arena" is one.

    Rather than having two entries, one as "clan" and the other as "arena", I would like "clan arena" to be one entry in the array.

    How would the best way be to go about this?

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,501

    Re: Spliting a string

    Are all the strings this convenient, ie. 1,1,1,1,2 words? That's doable. If it varies, eg. 2,1,1,1,1 or 1,1,1,1,1 then we've got a problem!

  3. #3
    vb Coda .paul.'s Avatar
    Join Date
    May 07
    Location
    Chelmsford UK
    Posts
    16,471

    Re: Spliting a string

    you can work around it with regex:

    vb.net Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Dim s As String = "tourney ffa team ctf ""clan arena"""
    7.         For Each str As String In Regex.Split(s, "(?<!""\w+?)\s(?!\w+?"")")
    8.             MsgBox(str.Replace("""", ""))
    9.         Next
    10.     End Sub
    11.  
    12. End Class

  4. #4
    Frenzied Member
    Join Date
    Sep 08
    Posts
    1,028

    Re: Spliting a string

    If it varies, eg. 2,1,1,1,1 or 1,1,1,1,1 then we've got a problem!
    Unfortunately... it varies

    Any other ideas on how to accomplish this?

    Maybe add all the items to the array and then call a 'Clean up' function that loops through the array, finds the positions of the 'clan' and 'arena' entries and then combines them into one entry.

    Could this be the way to do it?
    Last edited by Simon Canning; Aug 19th, 2012 at 11:25 PM.

  5. #5
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 11
    Location
    South Africa
    Posts
    865

    Re: Spliting a string

    If that is the only exception, then I would simply search my splitted string for clan and arena and merge the two, however if there are a lot more exceptions I would create a small tokenizer and parser seeing as your string doesn't have delimiters.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

Posting Permissions

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