当我们在RichTextBox中键入内容时,如何保存最后一个单词 [英] How to save the last word when we are typing in a RichTextBox

查看:73
本文介绍了当我们在RichTextBox中键入内容时,如何保存最后一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.
我有一个RichTextBox,用户可以在该组件中键入很多单词.
我有一个字符串.当用户键入一个单词时,我想将该单词保存在该字符串中,并且当用户按下空格键以写一个新单词时,该字符串的值应更改为null和agian.用户开始写一个新单词,这个字符串的值应该用这个新单词填充,并且该过程将一直持续到键入结束为止.
我该怎么办?请您帮帮我吗?
Best

Hi.
I have a RichTextBox which User can type lots of words in that component.
I have a string.When the user is typing a word,i want to save that word in that string and when the user press space in order to write a new word,this string`s value should be changed to null and agian when the user start to write a new word, this string`s value should be fill with this new word and this process will be continue until the end of typing.
What can i do?would you please help me?
Best

推荐答案

int prevInd = 0;
string lastWord = "";
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    if (richTextBox1.Text.Length == 0)
        return;
    if (richTextBox1.Text[richTextBox1.Text.Length - 1] == ' ')
    {
        lastWord = richTextBox1.Text.Substring(prevInd, richTextBox1.Text.Length - prevInd);
        prevInd += lastWord.Length;
        MessageBox.Show(lastWord);
    }
}


另一种方法,

Another way,

class Program
{
    static string GetLastWord(string dataToTest)
    {
        return String.IsNullOrEmpty(dataToTest) ? default(string) : dataToTest.Split(new char[] { ' ' }).LastOrDefault();
    }

    static void Main(string[] args)
    {

        string dataToTest = "Hello world I am a String!";
        string lastWord = GetLastWord(dataToTest);
    }
}



:)



:)


这篇关于当我们在RichTextBox中键入内容时,如何保存最后一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆