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.
Write like below
name = name.Replace(" ","");
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));
}
}
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>()
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With