gpt4 book ai didi

c# - 计算字符串中零的个数

转载 作者:行者123 更新时间:2023-11-30 13:10:17 25 4
gpt4 key购买 nike

我有一个字符串如下 51200000000000000000000000000000

这个字符串不是固定的。它将根据板的数量附加。如果有两个板,字符串将如下所示

我想知道如何计算字符串中零的个数。

我正在使用以下代码,但如果有两个以上的板,它就不灵活。

string str = "51200000000000000000000000000000";
string zeros = "00000000000000000000000000000";
if (str.Contains(zeros))
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}

最佳答案

您可以使用以下代码来执行此操作,它会为您提供零的数量 (Example)。

char matchChar='0';
string strInput = "51200000000000000000000000000000";
int zeroCount = strInput.Count(x => x == matchChar); // will be 29

您可以通过遍历每个字符并检查它是否是必需的字符(例如 0)然后计算它的数量来执行相同的操作。

关于c# - 计算字符串中零的个数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39070483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com