Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

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

Written by KSG01, December 19th, 2007
Code: ( text )
  1. Source
  2.  
  3. Public Function NoWhiteSpace(ByVal strText As String) As String
  4.    Return System.Text.RegularExpressions.Regex.Replace(strTe  xt, " ", _
  5.    String.Empty, System.Text.RegularExpressions.RegexOptions.Ignore  Case)
  6. End Function
  7.  
  8. Demo
  9.  
  10. Dim Test As String = " This is a simple test for No white spaces function "
  11. Console.WriteLine("The original text is [{0}] and the replacement is [{1}]", _
  12.       Test, RemoveWhiteSpace(Test))

1 Comment Posted ( Post your comment )
Plater / December 19th, 2007 09:10 PM
Hmmm, seems overkill for already created abilities.


This removes all spaces in the string
Code: ( text )
  1. Dim Test As String = " This is a simple test for No white spaces function "
  2. Test=Test.Replace(" ", String.Empty)


This removes leading/trailing spaces.
Code: ( text )
  1. Dim Test As String = " This is a simple test for No white spaces function "
  2. Test=Test.Trim(" ")

Stats:
Comments: 1