Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
It can be possible to count How many blank spaces in a single line of text?
How?
Posted

Use linq String.Count(), e.g.
C#
using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        string text = "Hello World...  Fine  Thanku.";
        int countSpaces = text.Count(Char.IsWhiteSpace);
        Console.WriteLine(countSpaces);
    }
}

Refer: String.Count Method[^]
 
Share this answer
 
v2
Using LINQ:
C#
string str="This is a sample string with blank spaces";
int spaceCount= str.Count(s => s == ' ');


Or,
C#
string str="This is a sample string with blank spaces";
int spaceCount= str.Split(' ').Length - 1;


Hope, it helps :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900