how to remove space in the middle using c#
35,361
Solution 1
Write like below
name = name.Replace(" ","");
Solution 2
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));
}
}
Solution 3
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>()
)
Author by
Kalaivani
Updated on July 28, 2022Comments
-
Kalaivani almost 2 years
how to remove space in the middle using c#? I have the
string name="My Test String"
and I need the output of the string as"MyTestString"
using c#. Please help me.