Get rid of special characters in a string using split - CSharp Language Basics

CSharp examples for Language Basics:string

Description

Get rid of special characters in a string using split

Demo Code

using System;//from ww w .  ja  v a2s  .c o  m
public class Program
{
   public static void Main(string[] strings)
   {
      char[] whiteSpace = {' ', '\n', '\t'};
      string s = " this is a\nstring";
      Console.WriteLine("before:" + s);
      Console.WriteLine("after:" + RemoveSpecialChars(s, whiteSpace));
   }
   public static string RemoveSpecialChars(string input, char[] targets)
   {
      string[] subStrings = input.Split(targets);
      string output = "";
      foreach(string subString in subStrings)
      {
         output = String.Concat(output, subString);
      }
      return output;
   }
}

Result


Related Tutorials