Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: Dec 2007
Location: Oregon USA
Posts: 1
#1   Dec 19 '07
Source
Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Function NoWhiteSpace(ByVal strText As String) As String
  3.    Return System.Text.RegularExpressions.Regex.Replace(strText, " ", _
  4.    String.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
  5. End Function
  6.  
Demo
Expand|Select|Wrap|Line Numbers
  1. Dim Test As String = " This is a simple test for No white spaces function "
  2. Console.WriteLine("The original text is [{0}] and the replacement is [{1}]", _
  3.       Test, RemoveWhiteSpace(Test))
  4.  

Last edited by Frinavale; Dec 10 '08 at 04:21 PM. Reason: removed non-code lines from code.



Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#2   Dec 19 '07

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


Hmmm, seems overkill for already created abilities.


This removes all spaces in the string
Expand|Select|Wrap|Line Numbers
  1. Dim Test As String = " This is a simple test for No white spaces function "
  2. Test=Test.Replace(" ", String.Empty)
  3.  
This removes leading/trailing spaces.
Expand|Select|Wrap|Line Numbers
  1. Dim Test As String = " This is a simple test for No white spaces function "
  2. Test=Test.Trim(" ")
  3.  
Reply