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.
how to remove space in the middle using c# [duplicate]
Asked Answered
Write like below
name = name.Replace(" ","");
How about name = name.Replace(" ", string.Empty); ? Same meaning, but more conventional :) –
Moorman
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>()
)
Avoid code only answers –
Corell
© 2022 - 2024 — McMap. All rights reserved.