2012-02-29 118 views

回答

4
Dim text = "My   name  is   Anas     Fares" 
Dim newText = System.Text.RegularExpressions.Regex.Replace(text, "[ ]{2,}", " ") 

或者,如果你想擺脫標籤,換行符等等等等等等

Dim text = "My   name  is  " & Environment.NewLine & " Anas     Fares" 
Dim newText = System.Text.RegularExpressions.Regex.Replace(text, "\s+", " ") 
+0

這是最好的:D – 2012-03-01 03:59:37

+0

+1對於每個答案。一切正確:D – 2012-03-01 04:00:00

+0

@JimThio:謝謝。很高興我幫了忙。 – LeftyX 2012-03-01 09:43:53

2

只需將做一個字符串替換上了雙白色的空間,一個空格:

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

A這一個直接轉換將是:

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, " ")