从 VB.NET 中的字符串中删除空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1645546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:29:53  来源:igfitidea点击:

Remove spaces from a string in VB.NET

vb.net

提问by l--''''''---------''''''''''''

How do you remove spaces from a string in VB.NET?

如何从 VB.NET 中的字符串中删除空格?

采纳答案by Joseph Lee

2015: Newer LINQ & lambda.

2015 年:更新的 LINQ 和 lambda。

  1. As this is an old Q (and Answer), just thought to update it with newer 2015 methods.
  2. The original "space" can refer to non-space whitespace (ie, tab, newline, paragraph separator, line feed, carriage return, etc, etc).
  3. Also, Trim() only remove the spaces from the front/back of the string, it does not remove spaces inside the string; eg: " Leading and Trailing Spaces " will become "Leading and Trailing Spaces", but the spaces inside are still present.
  1. 由于这是一个旧的 Q(和答案),只是想用更新的 2015 方法更新它。
  2. 原来的“空格”可以指非空格空格(即制表符、换行符、段落分隔符、换行符、回车符等)。
  3. 此外,Trim() 仅删除字符串前/后的空格,不会删除字符串内部的空格;例如:“Leading and Trailing Spaces”将变成“Leading and Trailing Spaces”,但里面的空格仍然存在。


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.

这将删除字符串内的所有(空白)空格、前导、尾随和。

回答by David

To remove ALL spaces:

删除所有空格:

 myString = myString.Replace(" ", "")

To remove leading andtrailing spaces:

要删除前导尾随空格:

myString = myString.Trim()

Note: this removes any white space, so newlines, tabs, etc. would be removed.

注意:这会删除任何空格,因此将删除换行符、制表符等。

回答by Jason S

"Spaces" in the original post could refer to whitespace, and no answer yet shows how to remove ALL whitespacefrom 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.aspxand http://msdn.microsoft.com/en-us/library/az24scfc.aspx

您可以在http://msdn.microsoft.com/en-us/library/hs600312.aspxhttp://msdn.microsoft.com/en-us/library/az24scfc.aspx 上找到有关 .NET 正则表达式的更多信息

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

回答by Brian Jasmer

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:

修剪字符串,使其在一行中不包含两个或多个空格。每个 2 个或更多空间的实例将被修剪为 1 个空间。一个简单的解决方案:

While ImageText1.Contains("  ")                     '2 spaces.
    ImageText1 = ImageText1.Replace("  ", " ")      'Replace with 1 space.
End While

回答by user1697111

What about Regex.Replace solution?

Regex.Replace 解决方案怎么样?

myStr = Regex.Replace(myStr, "\s", "")

回答by Lukek

This will remove spaces only, matches the SQL functionality of rtrim(ltrim(myString))

这将仅删除空格,匹配 rtrim(ltrim(myString)) 的 SQL 功能

Dim charstotrim() As Char = {" "c}
myString = myString .Trim(charstotrim) 

回答by Trish

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

回答by Ricardo Bernardino Wenger

Try this code for to trima String

试试这个代码到trim一个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