Tech Point Fundamentals

Thursday, December 16, 2021

C# Program to Delete the Special Characters from a String

C# Program to Delete the Special Characters from a String

coding-interview-question-csharp

Most of the IT companies check the coding skills and problem-solving skills as well along with the theoretical interview questions. Sometimes you are free to write the pseudo code and sometimes you are asked to write the complete program either on any paper or any editor. 


This question is asked in the coding interview to write the program. Here you can find the program as well as a live running program so that you can test the program immediately.


Watch our videos here





Question: 


How can you find and remove the special characters from a string? Write a program to remove the special characters from a string.




C# Program To Remove the Special Characters from a String


       
 

// Author 	: Tech Point Fundamentals
// Website 	: www.techpointfunda.com
// Channel	: https://www.youtube.com/c/TechPointFundamentals
// Question : How can you find and remove the special characters from a string? Write a program to remove the special characters from a string.
// Demo		: Removing Special Characters

using System;
using System.Text.RegularExpressions;
using System.Text;

public class Program
{
 public static void Main()
 {
   string inputString = "www.techpointfunda.com";		
   
   Console.WriteLine("Input String : " + inputString);		
   Console.WriteLine("After Removing Special Characters By Replace Method : " + RemovingSpecialCharactersByRegex(inputString));	
   Console.WriteLine("After Removing Special Characters By REGEX : " + RemovingSpecialCharactersByRegex(inputString));		
   Console.WriteLine("After Removing Special Characters By Loop : " + RemovingSpecialCharactersByLoop(inputString));
 }	  	
	
public static string RemovingSpecialCharacters(string inputString)    
{               
  string output = string.Empty;  
  
  output = inputString.Replace("[^a-zA-Z0-9]"," ");
  return output;
}  
	
public static string RemovingSpecialCharactersByLoop(string inputString)    
{               
  StringBuilder output = new StringBuilder();
  foreach (char c in inputString)
  {
  	if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
  	{
  		output.Append(c);
  	}
  }
  return output.ToString();
}    
	
public static string RemovingSpecialCharactersByRegex(string inputString) 
{
  return Regex.Replace(inputString, "[^a-zA-Z0-9]+", "", RegexOptions.Compiled);
}	
}





Output: 

Input String : www.techpointfunda.com
After Removing Special Characters By Replace Method : wwwtechpointfundacom
After Removing Special Characters By REGEX : wwwtechpointfundacom
After Removing Special Characters By Loop : wwwtechpointfundacom




Live Demo






No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.