using System; using System.Globalization; using System.Linq; using System.Collections.Generic; public class Test { public static void Main() { string text = "hello \\\"World\\\""; var builder = new System.Text.StringBuilder(); builder.Append(text[0] == '"' ? "\\\"" : text.Substring(0, 1)); for (int i = 1; i < text.Length; i++) { Char next = text[i]; Char last = text[i - 1]; if (next == '"' && last != '\\') { builder.Append("\\\""); } else builder.Append(next); } Console.WriteLine(builder.ToString()); } }