简体   繁体   English

使用正则表达式从 C# 字符串中删除所有空格

[英]Remove all whitespace from C# string with regex

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?我究竟做错了什么?

而不是RegEx使用Replace RegEx一些简单的事情:

LastName = LastName.Replace(" ", String.Empty);

Regex.Replace does not modify its first argument (recall that strings are immutable in .NET) so the call Regex.Replace不会修改它的第一个参数(回想一下字符串在 .NET 中是不可变的)所以调用

Regex.Replace(LastName, @"\s+", "");

leaves the LastName string unchanged.保持LastName字符串不变。 You need to call it like this:你需要这样称呼它:

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.但是,第一个正则表达式也会删除所有加号字符,我认为这是无意的。

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 .

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>()
                           )

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

You can write so, for exaple with phone number:您可以这样写,例如电话号码:

private static string GetFormattedPhoneNumber(string phoneNumber)
    => string.IsNullOrEmpty(phoneNumber) ? string.Empty 
    : Regex.Replace(phoneNumber, @"[+,\-,\(,\), \s+]*", string.Empty);

static void Main(string[] args)
    {
       Console.WriteLine(GetFormattedPhoneNumber("+375 (24) 55-333 "));
    }
/*
    Output: 3752455333
*/

就我而言,我让你删除空格:

   var year = System.Text.RegularExpressions.Regex.Replace(space, @"&nbsp;", string.Empty).Replace("Year: ", string.Empty).Trim();

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;

Why use Regex when you can simply use the Trim() method当您可以简单地使用 Trim() 方法时,为什么要使用 Regex

Text='<%# Eval("FieldDescription").ToString().Trim() %>'

OR要么

string test = "Testing    ";
test.Trim();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM