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

后端 未结 8 1396
感动是毒
感动是毒 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-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, "");
    

提交回复
热议问题