Removing all spaces from inside a string value in VB.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KSG01
    New Member
    • Dec 2007
    • 1

    Removing all spaces from inside a string value in VB.NET

    Source
    Code:
       
    Public Function NoWhiteSpace(ByVal strText As String) As String
       Return System.Text.RegularExpressions.Regex.Replace(strText, " ", _
       String.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
    End Function
    Demo
    Code:
    Dim Test As String = " This is a simple test for No white spaces function "
    Console.WriteLine("The original text is [{0}] and the replacement is [{1}]", _
          Test, RemoveWhiteSpace(Test))
    Last edited by Frinavale; Dec 10 '08, 04:21 PM. Reason: removed non-code lines from code.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Hmmm, seems overkill for already created abilities.


    This removes all spaces in the string
    [code=vbnet]
    Dim Test As String = " This is a simple test for No white spaces function "
    Test=Test.Repla ce(" ", String.Empty)
    [/code]

    This removes leading/trailing spaces.
    [code=vbnet]
    Dim Test As String = " This is a simple test for No white spaces function "
    Test=Test.Trim( " ")
    [/code]

    Comment

    • ejm3
      New Member
      • Mar 2010
      • 1

      #3
      I'm sure the first method works fine, but I needed a simple (Non-Programmer) way to replace " " with "+" for Google Maps. Plater's method was about as easy as it gets. Thanks

      Comment

      Working...