using System; using System.Collections.Generic; using System.Text; public class MyClass { public static void Main() { string s = @"wefdfmsad fsd fsd sdfsdf s fw eerf wef 45 tet h4687ir '' 45t4rg ' ' 'e rtg 45t45t " + "\"\"\"\"\"\"iunhoi\"ion\"oin\"oin\"\"oin \"\"\"\"p popompm \"\"\" wsrfs f we f34rfg 45gerg " + "\\popok\\pokpok \\ \\ \\ \\\\ po m \\\\ \\\\ompom rgt 4erg 45g`````oinp```ponpo`pom```````` " + "pomm´´´´´ ´ ´´pmpoom´´´´´pompom´´´´popio3m5rg 45 geh3 56htwge rt245g56ygergeggggggggge´´´´´´" + "’’’’’’’’ponpoompo’’’ ’ ’pom po lo kjbojb98inl ’’’pj09i op’09j’’’’09u0inuog’’’ ’’0o9unio o’’" + "fwr sdfe rtgefg e5 y4r gdfgdf ‘‘‘‘‘‘piojp p‘‘‘jpoj‘oi‘‘‘‘piojpu09j‘‘‘‘pojpoj09jhn‘‘‘09u0'9jp"; string s1; WL(EscapeString(s)); DateTime dt1 = DateTime.Now; for (int i = 0; i < 1000000; i++) { s1 = EscapeString(s); } WL(DateTime.Now - dt1); DateTime dt2 = DateTime.Now; for (int i = 0; i < 1000000; i++) { s1 = EscapeStringNew(s); } WL(DateTime.Now - dt2); RL(); } public static string EscapeString(string value) { value = value.Replace("\\", "\\\\"); value = value.Replace("\'", "\\\'"); value = value.Replace("\"", "\\\""); value = value.Replace("`", "\\`"); value = value.Replace("´", "\\´"); value = value.Replace("’", "\\’"); value = value.Replace("‘", "\\‘"); return value; } public static string EscapeStringNew(string value) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < value.Length; i++) { if (value[i] == '\\' || value[i] == '\'' || value[i] == '\"' || value[i] == '`' || value[i] == '´' || value[i] == '’' || value[i] == '‘') sb.Append("\\"); sb.Append(value[i]); } return sb.ToString(); } #region Helper methods private static void WL(object text, params object[] args) { Console.WriteLine(text.ToString(), args); } private static void RL() { Console.ReadLine(); } private static void Break() { System.Diagnostics.Debugger.Break(); } #endregion }