Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C统计所有发生的线路馈电和结转返回_C#_Count_Richtextbox - Fatal编程技术网

C# C统计所有发生的线路馈电和结转返回

C# C统计所有发生的线路馈电和结转返回,c#,count,richtextbox,C#,Count,Richtextbox,在C语言中,我正在用行号制作一个简单的文本编辑器。我想计算字符串中有效换行符的数量 我要数一数 \r \n \r\n 我该怎么做 或者更好的是,有人能给我指一篇关于如何对rtf框进行行号计数的文章吗 public static int CountStringOccurrences(string text, string pattern) { // Loop through all instances of the string 'text'.

在C语言中,我正在用行号制作一个简单的文本编辑器。我想计算字符串中有效换行符的数量

我要数一数

\r \n \r\n

我该怎么做


或者更好的是,有人能给我指一篇关于如何对rtf框进行行号计数的文章吗

public static int CountStringOccurrences(string text, string pattern)
        {
            // Loop through all instances of the string 'text'.
            int count = 0;
            int i = 0;
            while ((i = text.IndexOf(pattern, i)) != -1)
            {
                i += pattern.Length;
                count++;
            }
            return count;
        }

计算字符串的出现次数:

public static int CountStringOccurrences(string text, string pattern)
        {
            // Loop through all instances of the string 'text'.
            int count = 0;
            int i = 0;
            while ((i = text.IndexOf(pattern, i)) != -1)
            {
                i += pattern.Length;
                count++;
            }
            return count;
        }

以下是有关CodeProject的文章:


以下是有关CodeProject的文章:

计数行-

带有行号的RTB-

计数行-

带有行号的RTB-

注意:这个答案更多地与计算字符串中的行的抽象任务有关,而不是与GUI方面的事情有关。对于最初的提问者来说,它可能没有其他一些答案有用,但我怀疑它在不涉及GUI的类似情况下有用。如果有足够多的人认为它与此无关,我将删除它

我将使用一个已经知道行尾的现有类型,即TextReader,与我的LineReader类型结合使用:

或者,如果没有依赖项:

public IEnumerable<string> GetLines(string text)
{
    using (TextReader reader = new StringReader(text))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            return line;
        }
    }
}
请注意,这将计算实际的文本行,而不是换行符-这可能与您想要的略有不同,例如,通常换行符为+1,但如果文本末尾有换行符,则不会计算。

注意:这个答案更多地与计算字符串中的行的抽象任务有关,而不是处理GUI方面的事情。对于最初的提问者来说,它可能没有其他一些答案有用,但我怀疑它在不涉及GUI的类似情况下有用。如果有足够多的人认为它与此无关,我将删除它

public static int LineBreakCount(string s)
{
    if (s == null) throw new ArgumentNullException("s");

    return LineBreakCount(s, new[]{"\r\n", "\r", "\n"});
}

public static int LineBreakCount(string s, params string[] patterns)
{
    if (s == null) throw new ArgumentNullException("s");
    if (patterns == null) throw new ArgumentNullException("patterns");

    return s.Split(patterns, StringSplitOptions.None).Length;
}
我将使用一个已经知道行尾的现有类型,即TextReader,与我的LineReader类型结合使用:

或者,如果没有依赖项:

public IEnumerable<string> GetLines(string text)
{
    using (TextReader reader = new StringReader(text))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            return line;
        }
    }
}
请注意,这将计算实际的文本行,而不是换行符-这可能与您想要的略有不同,例如,通常为换行符+1,但如果文本末尾有换行符,则不会

public static int LineBreakCount(string s)
{
    if (s == null) throw new ArgumentNullException("s");

    return LineBreakCount(s, new[]{"\r\n", "\r", "\n"});
}

public static int LineBreakCount(string s, params string[] patterns)
{
    if (s == null) throw new ArgumentNullException("s");
    if (patterns == null) throw new ArgumentNullException("patterns");

    return s.Split(patterns, StringSplitOptions.None).Length;
}
第一个重载中模式的顺序很重要,因为如果您先执行\r\n或\r\n,则数组中的项目数将几乎增加一倍或正好增加一倍,因为它会按照指定的顺序执行这些项目


模式在第一个重载中的顺序很重要,因为如果您先这样做,\r或\n,数组中的项目数将几乎是或正好是原来的两倍,因为它会按照指定的顺序执行这些项目。

Chris,无法保证Split方法总是按照指定的顺序使用分隔符。这是一个实施细节。谢谢,卢克。在Snippet compiler中对一个有6个换行符的字符串运行它,得到的数组长度为11,在列表的末尾\r\n,在列表的开头为6。我只是假设它是按顺序执行的,所以任何使用此方法的人都应该将其放入循环中。Chris,不能保证Split方法总是按照指定的顺序使用分隔符。这是一个实施细节。谢谢,卢克。在Snippet compiler中对一个有6个换行符的字符串运行它,得到的数组长度为11,在列表的末尾\r\n,在列表的开头为6。我只是假设它是按顺序执行的,所以任何使用此方法的人都应该将其放入循环中。这本身就是一个很好的解决方案,但它还有一个额外的好处,即如果您需要知道字符串中有多少换行符,您可能还需要在某个点的换行符处拆分它。这本身就是一个很好的解决方案,但它还有一个额外的好处,即如果您需要知道一个字符串中有多少换行符,不管怎样,你可能也需要在某一点的断线处拆分它。ryan farley链接正是我需要的ty ryan farley链接正是我需要的ty