如何使用c#删除中间的空间

Kal*_*ani 7 c# removing-whitespace

如何使用c#删除中间的空间?我有string name="My Test String",我需要"MyTestString"使用c#的字符串输出.请帮我.

Sai*_*ala 36

写如下

name = name.Replace(" ","");
Run Code Online (Sandbox Code Playgroud)

  • 怎么样 name = name.Replace(" ", string.Empty); ? 相同的意思,但更传统:) (2认同)

Sun*_*van 8

using System;
using System.Text.RegularExpressions;

class TestProgram
{
    static string RemoveSpaces(string value)
    {
    return Regex.Replace(value, @"\s+", " ");
    }

    static void Main()
    {
    string value = "Sunil  Tanaji  Chavan";
    Console.WriteLine(RemoveSpaces(value));
    value = "Sunil  Tanaji\r\nChavan";
    Console.WriteLine(RemoveSpaces(value));
    }
}
Run Code Online (Sandbox Code Playgroud)