Solved String split on double and single space

Posted on 2009-12-17
Last Modified: 2012-05-08
Hi I have a string containing the following info  " 1 10  2"

note there are 3 pieces of information that I would like to get out into an array using split
the problem is that there are single and double blank spaces when I split on a space I get 4 fields which is not correct there should be 3. I also cant rely on there being a double space between the second and last info sometimes it will be between first and second or both . I can eliminat the initial space using Trim(string) just fine what is lef it the double spaces

Any suggestions as to how I could get around this ?

Question by:MichMat
    LVL 13

    Accepted Solution

           Dim sLine As String
            Dim iAt As Integer
            Dim strList As List(Of String) = New List(Of String)

            sLine = " 1 10  2"
            sLine = Trim(sLine)
            While (sLine <> "")
                iAt = sLine.IndexOf(" ")
                If iAt < 0 Then
                    sLine = ""
                    strList.Add(sLine.Substring(0, iAt))
                    sLine = Trim(sLine.Substring(iAt))
                End If
            End While

    Above code is tested.  strList will have 3 items.

    You can use following to see what is in strList
            For intCount = 0 To strList.Count - 1
                MessageBox.Show("Value = " + strList(intCount))

    LVL 18

    Assisted Solution

    Before splitting, try this:

    Dim input As String = " 1   10  2"
    Dim pattern As String = "(\s)*\s"
    Dim output As String = Regex.Replace(input, pattern, " ").Trim()

    Don't forget to add this at the top of your code:
    Import System.Text.RegularExpressions

    Author Closing Comment

    Thanks both for two great ideas

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now