删除字符串中多余的空格

发布于 2024-11-01 03:00:57 字数 176 浏览 3 评论 0原文

我想使用 VB.net ex 删除多余的空格

"The   Quick          Brown Fox"

我想要输出

"The Quick Brown Fox"

谢谢, 英奇卡

I want to remove the excess white spaces using VB.net

ex.

"The   Quick          Brown Fox"

I want output

"The Quick Brown Fox"

Thanks,
inchika

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情愿 2024-11-08 03:00:57

您可以使用一个简单的正则表达式来实现:

Dim cleaned As String = Regex.Replace(input, "\s{2,}", " ")

You can use a simple regular expression for that:

Dim cleaned As String = Regex.Replace(input, "\s{2,}", " ")
潦草背影 2024-11-08 03:00:57

我意识到这个问题相当老了,但还有另一个选项不涉及正则表达式,或手动循环字符串并替换:

Private Function StripSpaces(input As String) As String
    Return String.Join(" ", input.Split(New Char() {}, StringSplitOptions.RemoveEmptyEntries))
End Function

以及 C# 等效项:

private string StripSpaces(string input)
{
    return string.Join(" ", input.Split((char[])null, StringSplitOptions.RemoveEmptyEntries));
}

使用“null”值作为 String.Split 原因分割字符是所有在发送到 Char.IsWhiteSpace。因此,以这种方式调用该方法将在所有空格上拆分字符串,删除空字符串,然后将其重新连接在一起,并在每个拆分数组元素之间添加一个空格。

I realize that this question is fairly old, but there is another option that doesn't involve Regex, or manually looping through the string and replacing:

Private Function StripSpaces(input As String) As String
    Return String.Join(" ", input.Split(New Char() {}, StringSplitOptions.RemoveEmptyEntries))
End Function

And the C# equivalent:

private string StripSpaces(string input)
{
    return string.Join(" ", input.Split((char[])null, StringSplitOptions.RemoveEmptyEntries));
}

Using a "null" value as the split character on String.Split causes the split character to be all characters that return true if they were sent to Char.IsWhiteSpace. Therefore, calling the method this way will split your string on all whitespace, remove the empty strings, then re-join it together with a single space in between each split array element.

小傻瓜 2024-11-08 03:00:57

您真正想要的是将任何多个空格压缩为一个空格,一种方法是搜索两个空格并将它们替换为一个空格,直到没有两个相邻的空格为止,如下所示:

   Dim myString As String = "The   Quick     Brown     Fox"
   While myString.IndexOf("  ") <> -1
       myString = myString.Replace("  ", " ")
   End While
   Console.WriteLine(myString)

但是,这并不是万无一失的,因为 .net 字符串 的一些意识形态,这可能会发生进入无限循环,但仅限于一些非常奇怪的输入。


编辑:正如其他答案中所指出的,使用正则表达式这种特定的处理速度更快(也更简单)。

What you actually want is to compact any multiple white space to a single space, and one way to do that is to search for two spaces and replace them with a single space, until there are no two adjascent spaces left, something like this:

   Dim myString As String = "The   Quick     Brown     Fox"
   While myString.IndexOf("  ") <> -1
       myString = myString.Replace("  ", " ")
   End While
   Console.WriteLine(myString)

However, this is not fool-proof because of some ideosyncracies of .net strings, this might go into an endless loop, but only for some very odd inputs.


EDIT: This particular processing is faster (and simpler) using a regular expression, as pointed in the othe answers.

梦忆晨望 2024-11-08 03:00:57

试试这个:

Dim output As String = Regex.Replace("The   Quick          Brown Fox","\\s+" , " ")

Try this:

Dim output As String = Regex.Replace("The   Quick          Brown Fox","\\s+" , " ")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文