[c#] How to replace a char in string with an Empty character in C#.NET

I have a string like this:

string val = "123-12-1234";

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

I mean val.Replace(char oldChar, newChar);

What needs to go in oldChar and newChar.

This question is related to c#

The answer is


Since the other answers here, even though correct, do not explicitly address your initial doubts, I'll do it.

If you call string.Replace(char oldChar, char newChar) it will replace the occurrences of a character with another character. It is a one-for-one replacement. Because of this the length of the resulting string will be the same.

What you want is to remove the dashes, which, obviously, is not the same thing as replacing them with another character. You cannot replace it by "no character" because 1 character is always 1 character. That's why you need to use the overload that takes strings: strings can have different lengths. If you replace a string of length 1, with a string of length 0, the effect is that the dashes are gone, replaced by "nothing".


I have a latin version of the code of Razvan Dumitru because us use a million indicator even. Offcourse I use a double replace :D

public static string CleanNumb(string numb) 
{
    foreach (char c in ".,'ยด")
       numb = numb.Replace(c, ' ');

    return numb.Replace(" ", "");
}

It always bothered me that I can't use the String.Remove method to get rid of instances of a string or character in a string so I usually add theses extension methods to my code base:

public static class StringExtensions
{
    public static string Remove(this string str, string toBeRemoved)
    {
        return str.Replace(toBeRemoved, "");
    }

    public static string RemoveChar(this string str, char toBeRemoved)
    {
        return str.Replace(toBeRemoved.ToString(), "");

    }
}

The one taking char can't use overload semantics unfortunately since it will resolve to string.Remove(int startIndex) since it is "closer"

This is of course purely esthetics, but I like it...


val.Replace("-", "");

MSDN Source


This seems too simple, but:

val.Replace("-","");

If you are in a loop, let's say that you loop through a list of punctuation characters that you want to remove, you can do something like this:

      private const string PunctuationChars = ".,!?$";
          foreach (var word in words)
                {
                    var word_modified = word;

                    var modified = false;

                    foreach (var punctuationChar in PunctuationChars)
                    {
                        if (word.IndexOf(punctuationChar) > 0)
                        {
                            modified = true;
                            word_modified = word_modified.Replace("" + punctuationChar, "");


                        }
                    }
               //////////MORE CODE
               }

The trick being the following:

word_modified.Replace("" + punctuationChar, "");

string val = "123-12-1234";

val = val.Replace("-", ""); // result: 123121234

If you want to replace a char in a string with an empty char that means you want to remove that char from a string, read the answer of R. Martinho Fernandes.

Here is an exemple of how to remove a char from a string (replace with an "Empty char"):

    public static string RemoveCharFromString(string input, char charItem)
    {
        int indexOfChar = input.IndexOf(charItem);
        if (indexOfChar >= 0)
        {
            input = input.Remove(indexOfChar, 1);
        }
        return input;
    }

or this version that removes all recurrences of a char in a string :

    public static string RemoveCharFromString(string input, char charItem)
    {
        int indexOfChar = input.IndexOf(charItem);
        if (indexOfChar < 0)
        {
            return input;
        }
        return RemoveCharFromString(input.Remove(indexOfChar, 1), charItem);
    }