2012-02-29 73 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, " ")