How to remove space in the middle using C #

How to remove space in the middle using C #? I have a string name="My Test String" and I need the output of the string as "MyTestString" using C #. Please help me.

+6
source share
3 answers

Write as below

 name = name.Replace(" ",""); 
+30
source
 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)); } } 
+6
source

The fastest and most common way to do this (line terminators, tabs will also be processed). Regex powerful tools are not really needed to solve this problem, but Regex can reduce performance.

 new string (stringToRemoveWhiteSpaces .Where ( c => !char.IsWhiteSpace(c) ) .ToArray<char>() ) 
+1
source

Source: https://habr.com/ru/post/912044/


All Articles