Deux options de printemps à l'esprit:
- Utilisez un StringBuilder
- utiliser une expression régulière
Voici la version StringBuilder
:
public static string Trim(this string word, IEnumerable<char> selectedChars)
{
// The best form for this will depend largely on the size of selectedChars
// If you can change how you call the method, there are optimisations you
// could do here
HashSet<char> charSet = new HashSet<char>(selectedChars);
// Give enough capacity for the whole word. Could be too much,
// but definitely won't be too little
StringBuilder builder = new StringBuilder(word.Length);
foreach (char c in word)
{
if (!charSet.Contains(c))
{
builder.Append(c);
}
}
return builder.ToString();
}
L'option d'expression régulière pourrait être très efficace si vous avez une correction Ed jeu de caractères que vous souhaitez couper, et peut construire l'expression régulière une fois.
Quelque chose comme:
// Put this statically somewhere
Regex unwantedChars = new Regex("[def]", RegexOptions.Compiled);
// Then do this every time you need to use it:
word = unwantedChars.Replace(word, "");