Reputation: 60691
How do you remove spaces from a string in VB.NET?
Upvotes: 45
Views: 243367
Reputation: 199
What about Regex.Replace solution?
myStr = Regex.Replace(myStr, "\s", "")
Upvotes: 5
Reputation: 3043
2015: Newer LINQ & lambda.
Function RemoveWhitespace(fullString As String) As String
Return New String(fullString.Where(Function(x) Not Char.IsWhiteSpace(x)).ToArray())
End Function
This will remove ALL (white)-space, leading, trailing and within the string.
Upvotes: 24
Reputation: 1036
This will remove spaces only, matches the SQL functionality of rtrim(ltrim(myString))
Dim charstotrim() As Char = {" "c}
myString = myString .Trim(charstotrim)
Upvotes: 3
Reputation: 1
Try this code for to trim
a String
Public Function AllTrim(ByVal GeVar As String) As String
Dim i As Integer
Dim e As Integer
Dim NewStr As String = ""
e = Len(GeVar)
For i = 1 To e
If Mid(GeVar, i, 1) <> " " Then
NewStr = NewStr + Mid(GeVar, i, 1)
End If
Next i
AllTrim = NewStr
' MsgBox("alltrim = " & NewStr)
End Function
Upvotes: 0
Reputation: 151
To trim a string down so it does not contain two or more spaces in a row. Every instance of 2 or more space will be trimmed down to 1 space. A simple solution:
While ImageText1.Contains(" ") '2 spaces.
ImageText1 = ImageText1.Replace(" ", " ") 'Replace with 1 space.
End While
Upvotes: 15
Reputation: 31
You can also use a small function that will loop through and remove any spaces.
This is very clean and simple.
Public Shared Function RemoveXtraSpaces(strVal As String) As String
Dim iCount As Integer = 1
Dim sTempstrVal As String
sTempstrVal = ""
For iCount = 1 To Len(strVal)
sTempstrVal = sTempstrVal + Mid(strVal, iCount, 1).Trim
Next
RemoveXtraSpaces = sTempstrVal
Return RemoveXtraSpaces
End Function
Upvotes: 2
Reputation: 1544
"Spaces" in the original post could refer to whitespace, and no answer yet shows how to remove ALL whitespace from a string. For that regular expressions are the most flexible approach I've found.
Below is a console application where you can see the difference between replacing just spaces or all whitespace.
You can find more about .NET regular expressions at http://msdn.microsoft.com/en-us/library/hs600312.aspx and http://msdn.microsoft.com/en-us/library/az24scfc.aspx
Imports System.Text.RegularExpressions
Module TestRegExp
Sub Main()
' Use to match all whitespace (note the lowercase s matters)
Dim regWhitespace As New Regex("\s")
' Use to match space characters only
Dim regSpace As New Regex(" ")
Dim testString As String = "First Line" + vbCrLf + _
"Second line followed by 2 tabs" + vbTab + vbTab + _
"End of tabs"
Console.WriteLine("Test string :")
Console.WriteLine(testString)
Console.WriteLine("Replace all whitespace :")
' This prints the string on one line with no spacing at all
Console.WriteLine(regWhitespace.Replace(testString, String.Empty))
Console.WriteLine("Replace all spaces :")
' This removes spaces, but retains the tabs and new lines
Console.WriteLine(regSpace.Replace(testString, String.Empty))
Console.WriteLine("Press any key to finish")
Console.ReadKey()
End Sub
End Module
Upvotes: 18
Reputation: 73554
To remove ALL spaces:
myString = myString.Replace(" ", "")
To remove leading and trailing spaces:
myString = myString.Trim()
Note: this removes any white space, so newlines, tabs, etc. would be removed.
Upvotes: 91