Introducing Radical.sh

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

Replacing all except alpha numeric characters in C#

REGEX stands for Regular expression.
Using regex, we can replace the characters,numbers with a string.

Syntax:
  1. Regex regex = new Regex("[Regular expression]");
  2. output = regex.Replace("input_string", "substitution_string");


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13.  
  14. string filename = "My name is C-SHARP 007; (got it !)";
  15. //find all characters that are letters
  16. Regex regex = new Regex("[^a-zA-Z0-9]");
  17. filename = regex.Replace(filename, "$");
  18.  
  19. Console.WriteLine(filename);
  20. }
  21.  
  22. }
  23. }


Output:
  1. My$name$is$C$SHARP$007$$$got$it$$$