相关疑难解决方法(0)

你如何计算字符串中字符串(实际上是字符)的出现次数?

我正在做一些事情,我意识到我想要计算/我能在字符串中找到多少,然后它让我感到震惊,有几种方法可以做到,但无法决定最好的(或最简单的)是什么.

目前我正在做的事情如下:

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)

c# string

820
推荐指数
18
解决办法
59万
查看次数

在字符串 C# 中查找子字符串的计数

我试图找出“血清素”一词在收集的网络数据中出现的次数,但找不到查找次数的方法。

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 的计数

c# substring count indexof unity-game-engine

7
推荐指数
1
解决办法
9743
查看次数

从源字符串中查找多个索引

基本上我需要做String.IndexOf(),我需要从源字符串中获取索引数组.

有没有简单的方法来获取索引数组?

在提出这个问题之前,我已经搜索了很多,但还没有找到解决这个简单问题的简单解决方案.

.net c# string algorithm

6
推荐指数
3
解决办法
7493
查看次数

计算字符串中\ r\r \n的数量

我想算一下,我\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中.

c# string

0
推荐指数
1
解决办法
98
查看次数

标签 统计

c# ×4

string ×3

.net ×1

algorithm ×1

count ×1

indexof ×1

substring ×1

unity-game-engine ×1