Advertisement
Guest User

Untitled

a guest
Dec 7th, 2010
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.         public static string EscapeChars(string Input)
  12.         {
  13.             string Output = "";
  14.  
  15.             char lastChar = '0';
  16.             foreach (char c in Input)
  17.             {
  18.                 if (lastChar == '\\')
  19.                 {
  20.                     switch (c)
  21.                     {
  22.                         case 'n':
  23.                         case 'r':
  24.                         case 't':
  25.                             Output += @"\" + c;
  26.                             break;
  27.                     }
  28.                 }
  29.                 else
  30.                 {
  31.                     Output += c;
  32.                 }
  33.                 lastChar = c;
  34.             }
  35.  
  36.             return Output;
  37.         }
  38.         static void Main(string[] args)
  39.         {
  40.             string escaped = EscapeChars("Hello \n\r");
  41.             Console.WriteLine(escaped); //Prints "Hello" followed by two newlines.
  42.             Console.WriteLine(escaped == "Hello \n\r"); //Prints "true" -- no conversion was done.
  43.             Console.ReadKey();
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement