【问题标题】:Get the line number of a mutiline textbox that contains a string获取包含字符串的多行文本框的行号
【发布时间】:2014-07-14 15:50:29
【问题描述】:

例如,如果多行文本框在其中一行中有字符串“apple”,我如何获得行号?

【问题讨论】:

    标签: vb.net textbox contains


    【解决方案1】:

    另一种选择是使用具有GetLineFromCharIndex 方法的RichTextBox,您可以使用.Text.IndexOf 方法获取索引。

    【讨论】:

    • 我会使用富文本框。标准文本框是如此...慢...尝试以每秒 1000 行的速度将标准输出转储到文本框,而 Richtextbox :D 自定义 OnPaint 当然。
    【解决方案2】:

    你可以使用Array.IndexOf:

    Dim indexOfText = Array.IndexOf(textBox1.Lines, "apple")
    

    如果要查找可以作为行的一部分的字符串,也搜索不区分大小写:

    indexOfText = Array.FindIndex(textBox1.Lines, Function(str) str.IndexOf("apple", StringComparison.InvariantCultureIgnoreCase) >= 0)
    

    由于索引为零,因此如果需要行号,则需要加 1(以防索引不是 -1)。

    【讨论】:

    • 然后使用 RichTextBox 或 ListBox。除此之外,IndexOf 非常高效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    相关资源
    最近更新 更多