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

后端 未结 8 1290
感动是毒
感动是毒 2020-12-13 23:27

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

相关标签:
8条回答
  • 2020-12-13 23:56

    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".

    0 讨论(0)
  • 2020-12-14 00:01

    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, "");
    
    0 讨论(0)
  • 2020-12-14 00:03
    val.Replace("-", "");
    

    MSDN Source

    0 讨论(0)
  • 2020-12-14 00:05

    You can use a different overload of Replace() that takes string.

    val = val.Replace("-", string.Empty)
    
    0 讨论(0)
  • 2020-12-14 00:05
    string val = "123-12-1234";
    
    val = val.Replace("-", ""); // result: 123121234
    
    0 讨论(0)
  • 2020-12-14 00:08

    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);
        }
    
    0 讨论(0)
提交回复
热议问题