如何在富文本框中获取点击的单词编号(索引)



>假设

richTextBox1.Text = "Your description gives people the information they need to help you answer your question."

如果插入符号位置位于单词中:

  • information,我想得到6
  • gives,我想得到3
  • 。等等...

编辑:-感谢所有贡献者...

正如在答案中一样,有两种逻辑。

1-从开始到单击位置选择文本,然后使用(字符串。拆分)来拆分单词并计算它们。

var start = richTextBox1.SelectionStart;
var substring = richTextBox1.Text.Substring(0, start);
var wordscount = substring.Split(" ,.:;n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Length;
 Label1.Text = wordscount.ToString();

2-使用正则表达式获取单词标记化模式.Matches...然后比较匹配。使用点击位置编制索引

        int i = 1;
        string text = richTextBox1.Text;
        string tokenizingPattern = @"([[^][]*]|#[^#]*#)|s+";
        //Create lookup
        List<Tuple<string, int, int>> tokenizedWordLookup = new List<Tuple<string, int, int>>();
        tokenizedWordLookup.Add(Tuple.Create<string, int, int>("", i++, 1));
        foreach (Match match in Regex.Matches(text, tokenizingPattern, RegexOptions.Singleline))
            tokenizedWordLookup.Add(Tuple.Create<string, int, int>(match.Value, i++, match.Index));
        //Return the word index where the selection start is equal to the tokenizing word start
        Label1.Text = tokenizedWordLookup.LastOrDefault(x => x.Item3 <= richTextBox1.SelectionStart)?.Item2.ToString();

我建议这样做:

public Form1()
    {
        InitializeComponent();
        richTextBox1.Click += RichTextBox1_Click;
    }
    private void RichTextBox1_Click(object sender, EventArgs e)
    {
        var start = richTextBox1.SelectionStart;
        var substring = richTextBox1.Text.Substring(0, start);
        var words = substring.Split(new string[] { " ", "rn" }, StringSplitOptions.None);
        var count = words.Length;
        labelCurrentWordNumber.Text = count.ToString();
    }

当光标位于单词前面时,它将不包括单词。如果应该,请创建 StringSplitOptions.None

编辑:我添加了"\r"换行符,以增加每个单词的数量。但我认为你还必须过滤掉可能的东西,. ;等等,只计算字数。但这取决于你使用它的purpuse。

不如 Malior 的解决方案那么优雅,但这也很有效。

private int currentWordIndex()
{
    int currentWordIndex = 1;
    int charactersCounted = 1;
    if (richTextBox1.SelectionStart != 0)
    {
        foreach (Char character in richTextBox1.Text)
        {
            charactersCounted++;
            if (char.IsWhiteSpace(character))
                currentWordIndex++;
            if (charactersCounted == richTextBox1.SelectionStart)
                break;
        }
    }
    else
        currentWordIndex = 1;
    return currentWordIndex;
}

只需复制此方法并将 currentWordIndex() 添加到 richTextBox1_Click 事件中。

您可以遵循以下方法:

  1. 首先构造单词的查找
  2. 查找选择的开始和结束
  3. 获取单词索引

你可以从这里得到这个词标记化模式,由@Wiktor Stribiżew提供。

以下是完整的代码:

public Form1()
{
    InitializeComponent();
    richTextBox1.Click += RichTextBox1_Click;
}
private void RichTextBox1_Click(object sender, EventArgs e)
{
    var start = richTextBox1.SelectionStart;

    string text = richTextBox1.Text;
    string tokenizingPattern = @"([[^][]*]|#[^#]*#)|s+";
    // Create lookup
    List<Tuple<string, int, int, int>> tokenizedWordLookup = new List<Tuple<string, int, int, int>>();
    int i = 1;
    foreach (Match match in Regex.Matches(text, tokenizingPattern, RegexOptions.Singleline))
        tokenizedWordLookup.Add(Tuple.Create<string, int, int, int>(match.Value, i++, match.Index, match.Index + match.Length));
    // Find the word index where the selection start is equal to the tokenizing word start
    Tuple<string, int, int, int> foundTuple = (tokenizedWordLookup.Where(x => x.Item3 >= start && x.Item4 <= start).FirstOrDefault()) ?? Tuple<string, int, int, int> foundTuple
    labelCurrentWordNumber.Text = foundTuple.Item2.ToString();
}

最新更新