• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 858
  • Last Modified:

Vb.net String split on double and single space

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 ?

Michal
0
MichMat
Asked:
MichMat
2 Solutions
 
AshokCommented:
       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
                strList.Add(sLine)
                sLine = ""
            Else
                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))
        Next

HTH
Ashok
0
 
philipjonathanCommented:
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
0
 
MichMatAuthor Commented:
Thanks both for two great ideas
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now