Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Replacing \ with / using Regex.Replace in C#

Using Regex also, we can replace special characters.
While using Regex, the user must explicitly specify to ignore the special characters (Escape sequences).

  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Text.RegularExpressions;
  5.  
  6. public class ForgetCode
  7. {
  8. public static void Main()
  9. {
  10. string str = @"C:\home\level\one";
  11.  
  12. string newstr = Regex.Replace(str, @"\\", "/");
  13.  
  14. Console.WriteLine("Input: "+str);
  15. Console.WriteLine("Output: "+newstr);
  16. }
  17. }

Output:
Input: C:\home\level\one
Output: C:/home/level/one