C# 计算换行和回车的所有出现次数

发布于 2024-07-23 06:38:28 字数 156 浏览 4 评论 0 原文

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

我想数

\r \n \r\n

我该怎么做?

或者更好的是,有人可以向我指出一篇关于如何对 rtf 框进行行号的文章吗?

In C# i am making a simple text editor with line numbers. I want to count the ammount of valid line breaks in a string.

i want to count

\r
\n
\r\n

How can i do this?

Or better yet, can someone point me to an article on how to line number an rtf box

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦情居士 2024-07-30 06:38:28

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

我将使用已经知道行结尾的现有类型,即 TextReader,与来自 MiscUtil

string text = "ab\ncd";
int lines = new LineReader(() => new StringReader(text)).Count();

或者,没有依赖项:

public IEnumerable<string> GetLines(string text)
{
    using (TextReader reader = new StringReader(text))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            return line;
        }
    }
}

然后:

int lineCount = GetLines(text).Count();

请注意,这将计算实际的文本行而不是换行符 - 这可能与您想要的略有不同(例如,通常是换行符 + 1,但如果文本末尾有换行符则不是)。

Note: This answer is more to do with the abstract task of counting lines in a string, rather than to do with the GUI side of things. It's probably not as useful as some other answers for the original questioner, but I suspect it's useful in similar situations which don't involve GUIs. If enough people reckon it's not relevant here, I'll delete it.

I would use an existing type which already knows about line endings, namely TextReader, in conjunction with my LineReader type from MiscUtil:

string text = "ab\ncd";
int lines = new LineReader(() => new StringReader(text)).Count();

Alternatively, without the dependencies:

public IEnumerable<string> GetLines(string text)
{
    using (TextReader reader = new StringReader(text))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            return line;
        }
    }
}

then:

int lineCount = GetLines(text).Count();

Note that this will count actual lines of text rather than line breaks - this may subtly different from what you want (e.g. it'll normally be line breaks + 1, but not if there's a line break at the end of the text).

傾旎 2024-07-30 06:38:28

计算字符串出现的次数:

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;
        }

Counting occurrences of a string:

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;
        }
淡淡の花香 2024-07-30 06:38:28
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”,那么您最终会在数组中获得几乎或恰好两倍的项目,因为它在指定它们的顺序。

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;
}

The order of the patterns in the first overload is important, because if you do "\r" or "\n" first, you'll wind up with almost or exactly twice as many items in the array, since it performs them in the order they're specified.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文