Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old December 19th, 2007, 09:34 PM
Newbie
 
Join Date: Dec 2007
Location: Oregon USA
Posts: 1
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; December 10th, 2008 at 04:21 PM. Reason: removed non-code lines from code.



  #2  
Old December 19th, 2007, 10:10 PM
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,096

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