In C#, multiple ways exist to remove special characters from a given string. This is a significant problem that we often face when we try to compare two different strings. In this article, we are going to discuss three different ways to remove special characters from a given string.
Approach 1: Using Regular Expressions.
You can utilize regular expressions (Regex) to remove special characters from a string in C#. This method is flexible and allows you to specify which characters to keep or remove.
Example Code:
// C# program to remove special character using regular expression using System; using System.Text.RegularExpressions; public class HelloWorld { static string RemoveSpecialCharactersRegex(string input) { // Regular expression pattern to match special characters string pattern = @"[^a-zA-Z0-9\s]"; // Regex.Replace to remove special characters from the input string return Regex.Replace(input, pattern, ""); } public static void Main(string[] args) { string inputString = "Hello, @World! 123"; // Remove special characters from the input string using Regex string result = RemoveSpecialCharactersRegex(inputString); Console.WriteLine("Original String: " + inputString); Console.WriteLine("String with Special Characters Removed: " + result); } }
Original String: Hello, @World! 123
String with Special Characters Removed: Hello World 123
Approach 2: Using LINQ and Char.IsLetterOrDigit.
// C# code to remove speical characters from string using LINQ using System; using System.Linq; public class Program { static string RemoveSpecialCharactersLinq(string input) { // Use LINQ to filter out non-alphanumeric characters and spaces string cleanedString = new string(input.Where(char.IsLetterOrDigit).ToArray()); return cleanedString; } static void Main(string[] args) { string inputString = "Hello, @World! 123"; // Remove special characters from the input string using LINQ string result = RemoveSpecialCharactersLinq(inputString); Console.WriteLine("Original String: " + inputString); Console.WriteLine("String with Special Characters Removed: " + result); } }
Original String: Hello, @World! 123
String with Special Characters Removed: HelloWorld123
Approach 3: Using a String Builder.
// C# code to remove special character using stringbuider function using System; using System.Text; public class Program { static string RemoveSpecialCharactersStringBuilder(string input) { StringBuilder sb = new StringBuilder(); foreach (char c in input) { if (char.IsLetterOrDigit(c) || char.IsWhiteSpace(c)) { sb.Append(c); } } return sb.ToString(); } static void Main(string[] args) { string inputString = "Hello, @World! 123"; // Remove special characters from the input string using a StringBuilder string result = RemoveSpecialCharactersStringBuilder(inputString); Console.WriteLine("Original String: " + inputString); Console.WriteLine("String with Special Characters Removed: " + result); } }
Original String: Hello, @World! 123
String with Special Characters Removed: Hello World 123
No comments:
Post a Comment