使用正则表达式从 C# 字符串中删除所有空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16346146/
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
Remove all whitespace from C# string with regex
提问by BattlFrog
I am building a string of last names separated by hyphens. Sometimes a whitespace gets caught in there. I need to remove all whitespace from end result.
我正在构建一串由连字符分隔的姓氏。有时,空格会卡在那里。我需要从最终结果中删除所有空格。
Sample string to work on:
要处理的示例字符串:
Anderson -Reed-Smith
安德森-里德-史密斯
It needs to end up as (no space after Anderson):
它需要最终为(安德森之后没有空格):
Anderson-Reed-Smith
安德森-里德-史密斯
The last name string is in a string variable, LastName.
姓氏字符串位于字符串变量 LastName 中。
I am using a regular expression:
我正在使用正则表达式:
Regex.Replace(LastName, @"[\s+]", "");
The result of this is:
这样做的结果是:
Anderson -Reed-Smith.
安德森-里德-史密斯。
I also tried:
我也试过:
Regex.Replace(LastName, @"\s+", "");
and
和
Regex.Replace(LastName, @"\s", "");
What am I doing wrong?
我究竟做错了什么?
采纳答案by Gabe
Instead of a RegExuse Replacefor something that simple:
取而代之的是RegEx使用Replace的东西,很简单:
LastName = LastName.Replace(" ", String.Empty);
回答by I4V
No need for regex. This will also remove tabs, newlines etc
不需要正则表达式。这也将删除制表符、换行符等
var newstr = String.Join("",str.Where(c=>!char.IsWhiteSpace(c)));
WhiteSpace chars :0009 , 000a , 000b , 000c , 000d , 0020 , 0085 , 00a0 , 1680 , 180e , 2000 , 2001 , 2002 , 2003 , 2004 , 2005 , 2006 , 2007 , 2008 , 2009 , 200a , 2028 , 2029 , 202f , 205f , 3000.
空白字符:0009 , 000a , 000b , 000c , 000d , 0020 , 0085 , 00a0 , 1680 , 180e , 2000 , 2001 , 2002 , 2003 , 2004 , 2005 , 2006 , 2007 , 2008 , 2009 , 200a , 2028 , 2029 , 202f , 205f , 3000。
回答by dasblinkenlight
Regex.Replacedoes not modify its first argument (recall that strings are immutable in .NET) so the call
Regex.Replace不修改它的第一个参数(回想一下字符串在 .NET 中是不可变的)所以调用
Regex.Replace(LastName, @"\s+", "");
leaves the LastNamestring unchanged. You need to call it like this:
保持LastName字符串不变。你需要这样称呼它:
LastName = Regex.Replace(LastName, @"\s+", "");
All three of your regular expressions would have worked. However, the first regex would remove all plus characters as well, which I imagine would be unintentional.
您的所有三个正则表达式都可以使用。但是,第一个正则表达式也会删除所有加号字符,我认为这是无意的。
回答by user7329414
Below is the code that would replace the white space from the file name into given URL and also we can remove the same by using string.empty instead of "~"
下面是将文件名中的空格替换为给定 URL 的代码,我们也可以通过使用 string.empty 而不是“~”来删除相同的内容
if (!string.IsNullOrEmpty(url))
{
string origFileName = Path.GetFileName(url);
string modiFileName = origFileName.Trim().Replace(" ", "~");
url = url.Replace(origFileName , modiFileName );
}
return url;
回答by Tom McDonough
Why use Regex when you can simply use the Trim() method
当您可以简单地使用 Trim() 方法时,为什么要使用 Regex
Text='<%# Eval("FieldDescription").ToString().Trim() %>'
OR
或者
string test = "Testing ";
test.Trim();
回答by CSharpCoder
Fastest and general way to do this (line terminators, tabs will be processed as well). Regex powerful facilities don't really needed to solve this problem, but Regex can decrease performance.
执行此操作的最快和通用方法(行终止符,制表符也将被处理)。解决这个问题并不真正需要正则表达式强大的工具,但正则表达式会降低性能。
new string
(stringToRemoveWhiteSpaces
.Where
(
c => !char.IsWhiteSpace(c)
)
.ToArray<char>()
)
OR
或者
new string
(stringToReplaceWhiteSpacesWithSpace
.Select
(
c => char.IsWhiteSpace(c) ? ' ' : c
)
.ToArray<char>()
)
回答by pramod
Using REGEX you can remove the spaces in a string.
使用 REGEX 您可以删除字符串中的空格。
The following namespace is mandatory.
以下命名空间是必需的。
using System.Text.RegularExpressions;
Syntax:
句法:
Regex.Replace(text, @"\s", "")

