Removes every occurrence of the specified characters from the string. - CSharp Language Basics

CSharp examples for Language Basics:string

Description

Removes every occurrence of the specified characters from the string.

Demo Code

using System;//  w w w  .j  a  v a2  s  . c om
public class Program
{
   public static void Main(string[] args)
   {
      char[] whiteSpace = {' ', '\n', '\t'};
      string s = " this is a\nstring"; // Contains spaces & newline.
      Console.WriteLine("before:" + s);
      for(;;)
      {
         int offset = s.IndexOfAny(whiteSpace);
         if (offset == -1)
         {
            break;
         }
         string before = s.Substring(0, offset);
         string after  = s.Substring(offset + 1);
         s = String.Concat(before, after);
      }
      Console.WriteLine(s);
   }
}

Result


Related Tutorials