c# - 如何计算特定字符在其他特定字符第一次出现之前出现的频率?
问题描述
我有一个string
赞asdafahxlkax
。
我如何计算a
在第一次出现之前出现 3 次x
?
谢谢。
解决方案
尝试:
public int CountBeforeChar(char toCount, char beforeChar, string testString)
{
// Handle situation where we cannot find beforeChar in testString
var idx = testString.IndexOf(beforeChar);
idx = idx == -1 ? testString.Length - 1 : idx;
return testString
// Take substring until first occurence of "beforeChar"
.Substring(0, idx)
// Count all occurences of desired character in that substring
.Count(ch => ch == toCount);
}
推荐阅读
- angular - 使用 FormBuilder 和 formdata 以角度上传文件
- android - 分页无法正常工作 Android。它一次显示所有项目
- html - Flex 物品无法正确包裹和收缩
- python - Django 无法分配“'Pizza'”:“Order.Food_Name”必须是“Foods”实例
- android - 如何在 EditText DrawableRight 上执行点击监听?
- r - R如何将MultiLineString GeoJson文件转换为具有长列和纬度列的数据框?
- laravel - Laravel Passport 在 AuthServiceProvider 中抛出错误
- javascript - javascript 的对象通过引用传递的例外是什么?
- testing - 在 Idris 中测试证明是否合理
- python - nunique 不包括 pandas 中的一些值