Replace A Char With Empty String In C#

Replace A Char With Empty String In C#


You can replace a char in a string with an empty char in C# using string.Replace(char oldChar, char newChar )


Here is an example of how to remove a char from a string (replace with an empty char)


I have a string like this:


string text = "Lorem Ipsum is simply dummy text";


How can I replace the spaces using an empty string in C#


This question may be a simple question, but it is often made wrong.


To solve this problem, you can use the following code snippet.


	text = text.Replace(" ", string.Empty)

If you call string.Replace(char oldChar, char newChar), it will replace a character with another character.


Another way to remove all spaces from a string in C#


using System.Linq;
.
.
.
string text = " Lorem Ipsum is simply dummy text ";
string trimmedText = String.Concat(text.Where(x => !Char.IsWhiteSpace(x)));

//Result: "LoremIpsumissimplydummytext!"

We call the Where() LINQ method. That method filters the string's characters.


We join the "text" variable in the "trimmedText" variable with the String.Concat() method.


tag : c# string remove spaces,c# replace string empty char,c# string Replace(',', '')