Results 1 to 5 of 5

Thread: Parse a line of text

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Parse a line of text

    Does anyone have some code to parse a line of text to get rid of every occurance of a space or multiple spaces EXCEPT for the first time a space is encountered.

    Can use vb.net replace or a regular expression.

    Example: The dog is very big

    Result : The dogisverybig.

    Thanks.
    Last edited by djsnts; Apr 5th, 2015 at 05:43 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,407

    Re: Parse a line of text

    try this:

    Code:
    Dim s As String = "The dog is very big"
    Dim parts() As String = Split(s, " ", 2)
    parts(1) = parts(1).Replace(" ", "")
    MsgBox(String.Join(" ", parts))

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2014
    Posts
    82

    Re: Parse a line of text

    Thank you for your assistance. I would prefer using a regular expression if possible.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,407

    Re: Parse a line of text

    Code:
    Imports System.Text.RegularExpressions
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim s As String = "The dog is very big"
            MsgBox(Regex.Replace(s, "(?<!The)\s", ""))
        End Sub
    
    End Class

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,407

    Re: Parse a line of text

    this will work with any sentence:

    Code:
    Imports System.Text.RegularExpressions
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim s As String = "The dog is very big"
            MsgBox(Regex.Replace(s, "(?<=\s.*)\s", ""))
        End Sub
    
    End Class

Posting Permissions

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



Click Here to Expand Forum to Full Width