在一个文本行中不计算空格? [英] count no of spaces in one text line?

查看:129
本文介绍了在一个文本行中不计算空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以计算单行文本中有多少个空格?

如何?

It can be possible to count How many blank spaces in a single line of text?
How?

推荐答案

使用linq字符串.Count(),例如

Use linq String.Count(), e.g.
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);
    }
}



参考: String.Count方法 [ ^ ]


使用LINQ:

Using LINQ:
string str="This is a sample string with blank spaces";
int spaceCount= str.Count(s => s == ' ');





或者,



Or,

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





希望,它有助于:)



Hope, it helps :)


这篇关于在一个文本行中不计算空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆