fork download
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10. string text = "hello \\\"World\\\"";
  11. var builder = new System.Text.StringBuilder();
  12. builder.Append(text[0] == '"' ? "\\\"" : text.Substring(0, 1));
  13. for (int i = 1; i < text.Length; i++)
  14. {
  15. Char next = text[i];
  16. Char last = text[i - 1];
  17. if (next == '"' && last != '\\')
  18. {
  19. builder.Append("\\\"");
  20. }
  21. else
  22. builder.Append(next);
  23. }
  24. Console.WriteLine(builder.ToString());
  25. }
  26. }
Success #stdin #stdout 0.02s 34704KB
stdin
Standard input is empty
stdout
hello \"World\"