C# 计算字符串开头的空格数

C# 计算字符串开头的空格数,c#,string,count,whitespace,C#,String,Count,Whitespace,如何计算C#中字符串开头的空格数 例如: " this is a string" 结果是4。不知道如何正确地执行此操作 谢谢。您可以使用LINQ,因为string实现了IEnumerable: 使用Enumerable.TakeWhile,Char.IsWhiteSpace和Enumerable.Count int count = str.TakeWhile(Char.IsWhiteSpace).Count(); 请注意,不仅“是空白: 空白字符是以下Unicode字符: 空格分隔符

如何计算C#中字符串开头的空格数

例如:

"    this is a string"
结果是4。不知道如何正确地执行此操作


谢谢。

您可以使用LINQ,因为
string
实现了
IEnumerable


使用
Enumerable.TakeWhile
Char.IsWhiteSpace
Enumerable.Count

int count = str.TakeWhile(Char.IsWhiteSpace).Count();
请注意,不仅
是空白:

空白字符是以下Unicode字符:

  • 空格分隔符类别的成员,包括字符空格(U+0020)、OGHAM空格标记(U+1680)、蒙古语元音分隔符(U+180E)、EN-QUAD(U+2000)、EM-QUAD(U+2001)、EN-SPACE(U+2002)、EM-SPACE(U+2003)、3-PER-EM-SPACE(U+2004)、4-PER-EM-SPACE(U+2005)、6-PER-EM-SPACE(U+2006)、FIGURE-SPACE(U+2007),标点符号空间(U+2008)、精简空间(U+2009)、头发空间(U+200A)、窄不间断空间(U+202F)、中等数学空间(U+205F)和表意空间(U+3000)
  • LineSeparator类别的成员,该类别仅由行分隔符字符(U+2028)组成
  • 段落分隔符类别的成员,该类别仅由段落分隔符字符(U+2029)组成。字符字符表(U+0009)、换行符(U+000A)、换行符(U+000B)、换行符(U+000C)、回车符(U+000D)、下一行(U+0085)和不间断空格(U+00A0)

试试这个:

        static void Main(string[] args)
        {
            string s = "    this is a string";
            Console.WriteLine(count(s));
        }

        static int count(string s)
        {
            int total = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == ' ')
                    total++;
                else
                    break;
            }
            return total;
        }
static void Main(字符串[]args)
{
string s=“这是一个字符串”;
控制台写入线(计数);
}
静态整数计数(字符串s)
{
int-total=0;
对于(int i=0;i
虽然我喜欢基于Linq的答案,但这里有一个无聊的不安全方法,应该非常快

    private static unsafe int HowManyLeadingSpaces(string input)
    {
        if (input == null)
            return 0;
        if (input.Length == 0)
            return 0;
        if (string.IsNullOrWhiteSpace(input))
            return input.Length;

        int count = 0;
        fixed (char* unsafeChar = input)
        {

            for (int i = 0; i < input.Length; i++)
            {
                if (char.IsWhiteSpace((char)(*(unsafeChar + i))))
                    count++;
                else
                    break;
            }
        }

        return count;           


    }
私有静态不安全int howmanyladingspace(字符串输入)
{
如果(输入==null)
返回0;
if(input.Length==0)
返回0;
if(string.IsNullOrWhiteSpace(输入))
返回输入。长度;
整数计数=0;
固定(字符*未安全字符=输入)
{
for(int i=0;i
TrimStart解决方案是最简单的。@helrich,它会生成新字符串,所以它不是长字符串的最佳选择:)因此,我发布了两个解决方案,它们的功能都略有不同,因为
TrimStart
内部使用
Char.IsWhiteSpace
。在
input=“\t text”
上检查它。第一行返回1,第二行返回3,因为制表符被计为空白。第二行应该是
input.Length-input.TrimStart(''..Length
),以便与第一行具有相同的结果。@TimSchmelter感谢Tim,他不知道
TrimStart
\t
\r
视为空白,与MarcinJuraszek的答案中的LINQ等价物相比,这确实是很多代码。我不怀念那些没有林肯的糟糕日子假设您想要避免LINQ并使用原始C#,将的
更改为
,而
循环将比我需要的短得多,谢谢!感谢您提供有关空白的信息。
input.Length - input.TrimStart(' ').Length
        static void Main(string[] args)
        {
            string s = "    this is a string";
            Console.WriteLine(count(s));
        }

        static int count(string s)
        {
            int total = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == ' ')
                    total++;
                else
                    break;
            }
            return total;
        }
    private static unsafe int HowManyLeadingSpaces(string input)
    {
        if (input == null)
            return 0;
        if (input.Length == 0)
            return 0;
        if (string.IsNullOrWhiteSpace(input))
            return input.Length;

        int count = 0;
        fixed (char* unsafeChar = input)
        {

            for (int i = 0; i < input.Length; i++)
            {
                if (char.IsWhiteSpace((char)(*(unsafeChar + i))))
                    count++;
                else
                    break;
            }
        }

        return count;           


    }
 int count = 0, index = 0, lastIndex = 0;
  string s = "    this is a string";

  index = s.IndexOf(" ");
  while (index > -1) 
  {
    count++;
    index = s.IndexOf(" ", index + 1);

    if ((index - lastIndex) > 1)
      break;

    lastIndex = index;
  } 

  Console.WriteLine(count);