How to remove all space characters from a string?

Dear fellow programmers,

I am coding something in C # Visual Studio 2013, and I just realized that I do not need to use Trim()when I do Replace(" ", string.Empty).

Example:

SanitizedString = RawString
    .Replace("/", string.Empty)
    .Replace("\\", string.Empty)
    .Replace(" ", string.Empty)
    .Trim();

Since I previously had this code structured differently, I did not notice it:

SanitizedString = RawString.Trim()
    .Replace("/", string.Empty)
    .Replace("\\", string.Empty)
    .Replace(" ", string.Empty);

I know that these methods work differently, since it Trim()removes all whitespace, while it Replace(" ", string.Empty)only removes whitespace.

That is why I have another question.

I see no obvious way to achieve this with Replace. My question is, how can I do this when I want to remove all space characters from a string?

I found the following:

Efficient way to remove ALL spaces from a string?

, , ?

+7
7

Linq, :

  using System.Linq;

  ... 

  string source = "abc    \t def\r\n789";
  string result = string.Concat(source.Where(c => !char.IsWhiteSpace(c)));

  Console.WriteLine(result);

:

abcdef789
+15

- Regex

public static string ReplaceAllWhiteSpaces(string str) {
  return Regex.Replace(str, @"\s+", String.Empty);
}

: https://codereview.stackexchange.com/questions/64935/replace-each-whitespace-in-a-string-with-20

+5

, . , , .

, ( ), Unicode WSpace, , , WSpace.

, , \s, . , # regex , U+2001, , .

; , , , . ( : , ASCII), .

, . ( ), ( # - #, ).

+2

, .

 public static string RemoveSpaces(this String Value)
    {
        RegexOptions options = RegexOptions.None;
        Regex regex = new Regex(@"[ ]{2,}", options);
        return regex.Replace(Value.Trim(), @" ");
    }
+1

, , CodeProject, . , , ( ) :

public static string RemoveAllWhitespace(string str) {
    var len = str.Length;
    var src = str.ToCharArray();
    var dstIdx = 0;
    for (var i = 0; i < len; i++) {
        var ch = src[i];
        switch (ch) {
            case '\u0020': case '\u00A0': case '\u1680': case '\u2000': case '\u2001':
            case '\u2002': case '\u2003': case '\u2004': case '\u2005': case '\u2006':
            case '\u2007': case '\u2008': case '\u2009': case '\u200A': case '\u202F':
            case '\u205F': case '\u3000': case '\u2028': case '\u2029': case '\u0009':
            case '\u000A': case '\u000B': case '\u000C': case '\u000D': case '\u0085':
                break;
            default:
                src[dstIdx++] = ch;
                break;
        }
    }
    return new string(src, 0, dstIdx);
}

, , , , , (, RegEx ).

: https://www.codeproject.com/Articles/1014073/Fastest-method-to-remove-all-whitespace-from-Strin

: CodeProject. -.

+1
   string Abcd = Console.ReadLine();
        string wspace = "";
                    int len = Abcd.Length;
        for (int i = 0; i <= len-1; i++)
        {
            if (Abcd[i] != ' ')
            {
                wspace = wspace + Abcd[i];
            }

        }
        Console.WriteLine("The Sring Without Space Is= '"+wspace+"'");
        Console.ReadLine();
0

:

string message = Console.ReadLine().Trim().ToLower();

string message_without_space = message.Split(' ').Aggregate((a, b) => a + b).ToString();
0
source

All Articles