count words in this string - CSharp System

CSharp examples for System:String Search

Description

count words in this string

Demo Code


using System.Text.RegularExpressions;
using System.Linq;
using System.Collections.Generic;
using System;/*from  w w w  .  ja v a  2s  . c om*/

public class Main{
        /// <summary>
        /// count words in this string
        /// </summary>
        /// <param name="str">string</param>
        /// <returns>int</returns>
        public static int WordCount(this string str)
        {
            return str.Split(new[] {' ', '.', '?', '!'}, StringSplitOptions.RemoveEmptyEntries).Length;
        }
}

Related Tutorials