Wednesday 20 January 2016

C# String Split by any whitespace character Example

In this example we see how we could play with multiple whitespace  characters in side split function in C#.

       

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Split_By_Any_Char
{
    class Program
    {
        static void Main(String[] args)
        {
            string str = Console.ReadLine();// "He is a very very good boy, isn't he?";
            var arr = str.Split(("![,?.\\~ '@+]").ToArray());
            Console.WriteLine(arr.Where(a=> a.Trim() !="").Count());//Total no of word
            foreach (var c in arr)
            {
                if (c.Trim() != "")
                {
                    Console.WriteLine(c);
                }
            }
            Console.Read();
        }
    }
}


Output: