我正在做一些事情,我意识到我想要计算/我能在字符串中找到多少,然后它让我感到震惊,有几种方法可以做到,但无法决定最好的(或最简单的)是什么.
目前我正在做的事情如下:
string source = "/once/upon/a/time/";
int count = source.Length - source.Replace("/", "").Length;
Run Code Online (Sandbox Code Playgroud)
但我完全不喜欢它,任何接受者?
我真的不想挖掘RegEx这个,是吗?
我知道我的字符串将会有我正在搜索的术语,所以你可以认为......
当然,对于字符串,其中 长度> 1,
string haystack = "/once/upon/a/time";
string needle = "/";
int needleCount = ( haystack.Length - haystack.Replace(needle,"").Length ) / needle.Length;
Run Code Online (Sandbox Code Playgroud) 我试图找出“血清素”一词在收集的网络数据中出现的次数,但找不到查找次数的方法。
IEnumerator OnMouseDown()
{
string GatheredData;
StringToFind = "Serotonin"
string url = "https://en.wikipedia.org/wiki/Dopamine";
WWW www = new WWW(url);
yield return www;
GatheredData = www.text;
//Attempted methods below
M1_count = GatheredData.Contains(StringToFind);
M1_count = GatheredData.Count(StringToFind);
M1_count = GatheredData.IndexOf(StringToFind);
}
Run Code Online (Sandbox Code Playgroud)
当我告诉它索引和方法 2 中的哪个数字有效但仅适用于字符而不适用于字符串时,我可以轻松使用来自方法 1 和方法 3 的数据
我已经在网上和这里检查过,但没有发现找到 StringToFind 的计数
基本上我需要做String.IndexOf(),我需要从源字符串中获取索引数组.
有没有简单的方法来获取索引数组?
在提出这个问题之前,我已经搜索了很多,但还没有找到解决这个简单问题的简单解决方案.
我想算一下,我\r\r在一个字符串变量中的频率.
例如:
string sampleString = "9zogl22n\r\r\nv4bv79gy\r\r\nkaz73ji8\r\r\nxw0w91qq\r\r\ns05jxqxx\r\r\nw08qsxh0\r\r\nuyggbaec\r\r\nu2izr6y6\r\r\n106iha5t\r";
Run Code Online (Sandbox Code Playgroud)
结果将在此示例8中.