Replace multiple string elements in C #

Is there a better way to do this ...

MyString.Trim().Replace("&", "and").Replace(",", "").Replace(" ", " ") .Replace(" ", "-").Replace("'", "").Replace("/", "").ToLower(); 

I have expanded the class of strings to prevent it from reaching one job, but is there a faster way?

 public static class StringExtension { public static string clean(this string s) { return s.Replace("&", "and").Replace(",", "").Replace(" ", " ") .Replace(" ", "-").Replace("'", "").Replace(".", "") .Replace("eacute;", "Γ©").ToLower(); } } 



Just for fun (and for stopping the arguments in the comments) I popped the entity by comparing various examples below.

https://gist.github.com/ChrisMcKee/5937656

The regex parameter is terribly evaluated; Dictionary selection is the fastest; the long-winded version of the string substitute is slightly faster than the short one.

+54
string immutability c # refactoring
Aug 24 '09 at 9:25
source share
8 answers

Faster - no. More efficient - yes, if you use the StringBuilder class. With your implementation, each operation generates a copy of the string, which in circumstances may degrade performance. Strings are immutable objects, so each operation simply returns a modified copy.

If you expect this method to be actively called with a large number of Strings considerable length, it might be better to "port" its implementation to the StringBuilder class. Moreover, any modification is performed directly on this instance, so you perform unnecessary copy operations.

 public static class StringExtention { public static string clean(this string s) { StringBuilder sb = new StringBuilder (s); sb.Replace("&", "and"); sb.Replace(",", ""); sb.Replace(" ", " "); sb.Replace(" ", "-"); sb.Replace("'", ""); sb.Replace(".", ""); sb.Replace("eacute;", "Γ©"); return sb.ToString().ToLower(); } } 
+79
Aug 24 '09 at 9:27
source share

Maybe a little read?

  public static class StringExtension { private static Dictionary<string, string> _replacements = new Dictionary<string, string>(); static StringExtension() { _replacements["&"] = "and"; _replacements[","] = ""; _replacements[" "] = " "; // etc... } public static string clean(this string s) { foreach (string to_replace in _replacements.Keys) { s = s.Replace(to_replace, _replacements[to_replace]); } return s; } } 

Also add New In Town's suggestion about StringBuilder ...

+10
Aug 24 '09 at 9:33
source share

it will be more efficient:

 public static class StringExtension { public static string clean(this string s) { return new StringBuilder(s) .Replace("&", "and") .Replace(",", "") .Replace(" ", " ") .Replace(" ", "-") .Replace("'", "") .Replace(".", "") .Replace("eacute;", "Γ©") .ToString() .ToLower(); } } 
+8
Aug 24 '09 at 9:31
source share

If you are just after a beautiful decision and don’t need to save a few nanoseconds, how about some LINQ sugar?

 var input = "test1test2test3"; var replacements = new Dictionary<string, string> { { "1", "*" }, { "2", "_" }, { "3", "&" } }; var output = replacements.Aggregate(input, (current, replacement) => current.Replace(replacement.Key, replacement.Value)); 
+3
May 6 '14 at 2:21
source share

In the proposed solutions one can be optimized. Having many calls to Replace() , the code performs several passes on the same line. With very long strings, solutions can be slow due to lack of processor bandwidth. Maybe you should consider replacing multiple rows in a single pass .

+3
May 21 '14 at 13:21
source share

I am doing something similar, but in my case I am doing serialization / de-serialization, so I need to be able to go both ways. I find that using the string [] [] works almost the same with the dictionary, including initialization, but you can also go in another direction, returning the placeholders to their original values, because the dictionary is really not configured to run.

Edit: you can use Dictionary<Key,List<Values>> to get the same result as string [] []

+1
Sep 16 2018-11-22T00:
source share

Another use case for linq is

 [TestMethod] public void Test() { var input = "it worth a lot of money, if you can find a buyer."; var expected = "its worth a lot of money if you can find a buyer"; var removeList = new string[] { ".", ",", "'" }; var result = input; removeList.ToList().ForEach(o => result = result.Replace(o, string.Empty)); Assert.AreEqual(expected, result); } 
+1
Nov 09 '17 at 11:15
source share
 string input = "it worth a lot of money, if you can find a buyer."; for (dynamic i = 0, repl = new string[,] { { "'", "''" }, { "money", "$" }, { "find", "locate" } }; i < repl.Length / 2; i++) { input = input.Replace(repl[i, 0], repl[i, 1]); } 
-one
Mar 16 '17 at 0:23
source share



All Articles