一个字符在字符串中出现的次数 [英] Number of occurrences of a character in a string
本文介绍了一个字符在字符串中出现的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取以下字符串中某个字符(例如 &
)的出现次数.
I am trying to get the number of occurrences of a certain character such as &
in the following string.
string test = "key1=value1&key2=value2&key3=value3";
如何确定上述测试字符串变量中有 2 个与号 (&)?
How do I determine that there are 2 ampersands (&) in the above test string variable?
推荐答案
你可以这样做:
int count = test.Split('&').Length - 1;
或者使用 LINQ:
test.Count(x => x == '&');
这篇关于一个字符在字符串中出现的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文