从C#string中删除字符

我怎样才能从string中删除字符? 例如: "My name @is ,Wan.;'; Wan"

我想从string中删除字符'@', ',', '.', ';', '\'' ,使它变成"My name is Wan Wan"

 var str = "My name @is ,Wan.;'; Wan"; var charsToRemove = new string[] { "@", ",", ".", ";", "'" }; foreach (var c in charsToRemove) { str = str.Replace(c, string.Empty); } 

但是如果你想删除所有非字母字符,我可能会提出另一种方法

 var str = "My name @is ,Wan.;'; Wan"; str = new string((from c in str where char.IsWhiteSpace(c) || char.IsLetterOrDigit(c) select c ).ToArray()); 

简单:

 String.Join("", "My name @is ,Wan.;'; Wan".Split('@', ',' ,'.' ,';', '\'')); 

听起来像RegEx的理想应用程序 – 一个专为快速文本操作而devise的引擎。 在这种情况下:

 Regex.Replace("He\"ll,o Wo'r.ld", "[@,\\.\";'\\\\]", string.Empty) 

对于您的问题不太具体,可以通过在正则expression式中列出可接受的字符来删除string(空格除外)中的所有标点符号:

 string dirty = "My name @is ,Wan.;'; Wan"; // only space, capital AZ, lowercase az, and digits 0-9 are allowed in the string string clean = Regex.Replace(dirty, "[^A-Za-z0-9 ]", ""); 

请注意,之后有一个空格,以免删除句子中的空格。 第三个参数是一个空string,用来replace不属于正则expression式的任何子string。

  string x = "My name @is ,Wan.;'; Wan"; string modifiedString = x.Replace("@", "").Replace(",", "").Replace(".", "").Replace(";", "").Replace("'", ""); 

最简单的方法是使用String.Replace

 String s = string.Replace("StringToReplace", "NewString"); 

另一个简单的方法

 var forbiddenChars = @"@,.;'".ToCharArray(); var dirty = "My name @is ,Wan.;'; Wan"; var clean = new string(dirty.Where(c => !forbiddenChars.Contains(c)).ToArray()); 
 new List<string> { "@", ",", ".", ";", "'" }.ForEach(m => str = str.Replace(m, "")); 

一个string只是一个字符数组,所以使用Linq做replace(类似于上面的Albin,除了使用linq contains语句做replace):

 var resultString = new string( (from ch in "My name @is ,Wan.;'; Wan" where ! @"@,.;\'".Contains(ch) select ch).ToArray()); 

第一个string是replace字符的string,第二个string是包含字符的简单string

我不妨把它扔到这里。

做一个扩展从string中删除字符:

 public static string RemoveChars(this string input, params char[] chars) { var sb = new StringBuilder(); for (int i = 0; i < input.Length; i++) { if (!chars.Contains(input[i])) sb.Append(input[i]); } return sb.ToString(); } 

它可以这样使用:

 string str = "My name @is ,Wan.;'; Wan"; string cleanedUpString = str.RemoveChars('@', ',', '.', ';', '\''); 

或者就像这样:

 string str = "My name @is ,Wan.;'; Wan".RemoveChars('@', ',', '.', ';', '\''); 

很多好的答案在这里,这里是我的补充以及几个unit testing,可以用来帮助testing正确性,我的解决scheme类似于@ Rianne的上面,但使用一个ISet提供O(1)replace字符的查找时间(还类似于@Albin Sunnanbo的Linq解决scheme)。

  using System; using System.Collections.Generic; using System.Linq; /// <summary> /// Returns a string with the specified characters removed. /// </summary> /// <param name="source">The string to filter.</param> /// <param name="removeCharacters">The characters to remove.</param> /// <returns>A new <see cref="System.String"/> with the specified characters removed.</returns> public static string Remove(this string source, IEnumerable<char> removeCharacters) { if (source == null) { throw new ArgumentNullException("source"); } if (removeCharacters == null) { throw new ArgumentNullException("removeCharacters"); } // First see if we were given a collection that supports ISet ISet<char> replaceChars = removeCharacters as ISet<char>; if (replaceChars == null) { replaceChars = new HashSet<char>(removeCharacters); } IEnumerable<char> filtered = source.Where(currentChar => !replaceChars.Contains(currentChar)); return new string(filtered.ToArray()); } 

NUnit(2.6+)testing在这里

 using System; using System.Collections; using System.Collections.Generic; using NUnit.Framework; [TestFixture] public class StringExtensionMethodsTests { [TestCaseSource(typeof(StringExtensionMethodsTests_Remove_Tests))] public void Remove(string targetString, IEnumerable<char> removeCharacters, string expected) { string actual = StringExtensionMethods.Remove(targetString, removeCharacters); Assert.That(actual, Is.EqualTo(expected)); } [TestCaseSource(typeof(StringExtensionMethodsTests_Remove_ParameterValidation_Tests))] public void Remove_ParameterValidation(string targetString, IEnumerable<char> removeCharacters) { Assert.Throws<ArgumentNullException>(() => StringExtensionMethods.Remove(targetString, removeCharacters)); } } internal class StringExtensionMethodsTests_Remove_Tests : IEnumerable { public IEnumerator GetEnumerator() { yield return new TestCaseData("My name @is ,Wan.;'; Wan", new char[] { '@', ',', '.', ';', '\'' }, "My name is Wan Wan").SetName("StringUsingCharArray"); yield return new TestCaseData("My name @is ,Wan.;'; Wan", new HashSet<char> { '@', ',', '.', ';', '\'' }, "My name is Wan Wan").SetName("StringUsingISetCollection"); yield return new TestCaseData(string.Empty, new char[1], string.Empty).SetName("EmptyStringNoReplacementCharactersYieldsEmptyString"); yield return new TestCaseData(string.Empty, new char[] { 'A', 'B', 'C' }, string.Empty).SetName("EmptyStringReplacementCharsYieldsEmptyString"); yield return new TestCaseData("No replacement characters", new char[1], "No replacement characters").SetName("StringNoReplacementCharactersYieldsString"); yield return new TestCaseData("No characters will be replaced", new char[] { 'Z' }, "No characters will be replaced").SetName("StringNonExistantReplacementCharactersYieldsString"); yield return new TestCaseData("AaBbCc", new char[] { 'a', 'C' }, "ABbc").SetName("CaseSensitivityReplacements"); yield return new TestCaseData("ABC", new char[] { 'A', 'B', 'C' }, string.Empty).SetName("AllCharactersRemoved"); yield return new TestCaseData("AABBBBBBCC", new char[] { 'A', 'B', 'C' }, string.Empty).SetName("AllCharactersRemovedMultiple"); yield return new TestCaseData("Test That They Didn't Attempt To Use .Except() which returns distinct characters", new char[] { '(', ')' }, "Test That They Didn't Attempt To Use .Except which returns distinct characters").SetName("ValidateTheStringIsNotJustDistinctCharacters"); } } internal class StringExtensionMethodsTests_Remove_ParameterValidation_Tests : IEnumerable { public IEnumerator GetEnumerator() { yield return new TestCaseData(null, null); yield return new TestCaseData("valid string", null); yield return new TestCaseData(null, new char[1]); } } 

旧地方复制/踩踏:

  private static string RemoveDirtyCharsFromString(string in_string) { int index = 0; int removed = 0; byte[] in_array = Encoding.UTF8.GetBytes(in_string); foreach (byte element in in_array) { if ((element == ' ') || (element == '-') || (element == ':')) { removed++; } else { in_array[index] = element; index++; } } Array.Resize<byte>(ref in_array, (in_array.Length - removed)); return(System.Text.Encoding.UTF8.GetString(in_array, 0, in_array.Length)); } 

不确定其他方法的效率(即所有函数调用和实例在C#执行中发生的副作用的开销)。