答案 0 :(得分:5)
Dim text = "My name is Anas Fares"
Dim newText = System.Text.RegularExpressions.Regex.Replace(text, "[ ]{2,}", " ")
如果你想摆脱TABS,NEWLINES等等,还是这个。
Dim text = "My name is " & Environment.NewLine & " Anas Fares"
Dim newText = System.Text.RegularExpressions.Regex.Replace(text, "\s+", " ")
答案 1 :(得分:3)
你只需要在双白空间上替换一个空格:
Dim str = "Lorem Ipsum Dolar"
str = str.replace(" ", " ")
return str 'Lorem Ipsum Dolar
但您仍然可以在VB.NET中使用与链接中的javascript代码中使用的相同的正则表达式。本文展示了一种方法:
http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx
Dim input As String = "This is text with far too much " + _
"whitespace."
Dim pattern As String = "\s+"
Dim replacement As String = " "
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(input, replacement)
Console.WriteLine("Original String: {0}", input)
Console.WriteLine("Replacement String: {0}", result)
答案 2 :(得分:3)
直接转换是:
Dim r As Regex = New Regex("\s{2,}") 'or " {2,}" or "[ ]{2,}", depending on if you want whitespace, or just spaces.
Dim stringWithDoubleSpaces As String = "this is a double spaced string"
Dim stringWithSingleSpaces As String = r.Replace(stringWithDoubleSpaces, " ")