gpt4 book ai didi

c# - 获取在RichTextBox中输入的最后一个词c#

转载 作者:太空宇宙 更新时间:2023-11-03 21:43:30 25 4
gpt4 key购买 nike

如何使用 c# 在 Winform RichTextBox 中输入最后一个单词(两个空格字符之间的单词或者它应该考虑换行符、段落或制表符)及其开始位置和结束位置。我需要在按下空格键后立即得到最后一个词

我的代码(不能正常工作):

 private Word GetLastEnteredWord()
{

string _word = " ";
int pos = rtfText.SelectionStart;
Word word=new Word(_word,pos,0);
if (pos > 1)
{
string tmp = "";
var f = new char();
while (f != ' ' && f != 10 && pos > 0)
{
pos--;
tmp = rtfText.Text.Substring(pos, 1);

f = tmp[0];
_word += f;


}

char[] ca = _word.ToCharArray();
Array.Reverse(ca);
_word = new String(ca);
word.RWord = _word;
word.Si = pos;
word.Length = _word.Length;


}

return word;
}


public class Word
{
public Word(string word, int starti, int len)
{
RWord = word; //word
Si = starti; //start index
Length = len;
}

public string RWord { get; set; }
public int Si { get; set; }
public int Length { get; set; }
}

最佳答案

Substring() 方法做一个小技巧:

//KeyPress event handler for your richTextBox
private void richTextBox_KeyPress(object sender, KeyPressEventArgs e){
if(e.KeyChar == ' '){
int i = richTextBox.Text.TrimEnd().LastIndexOf(' ');
if(i != -1) MessageBox.Show(richTextBox.Text.Substring(i+1).TrimEnd());
}
}

关于c# - 获取在RichTextBox中输入的最后一个词c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18222988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com