获取文本框中最新输入的单词 Windows phone 8

Get the latest entered word in textbox Windows phone 8

我正在开发一个 Windows phone 应用程序。在我的应用程序中,我想在文本框中获取最新输入的单词而不是最后一个单词。我想更改按下 space 键时最新输入的单词。我在这样的按键事件上得到了最后的结论:

private async void mytxt_KeyUp_1(object sender, KeyRoutedEventArgs e)
{
   if (e.Key == Windows.System.VirtualKey.Space || e.Key == Windows.System.VirtualKey.Enter)
        { 
           if (string.IsNullOrWhiteSpace(textBox_string) == false)
              {
                  string[] last_words = Regex.Split(textBox_string, @"\s+");
                  int i = last_words.Count();
                  last_words = last_words.Where(x => x != last_words[i-1]).ToArray();               last_word = last_words[last_words.Count() - 1];
                  last_word = last_word.TrimStart();
               }
         }
}

我正在通过这种方法获取最后一个词,但实际上我想获取用户最新输入的词。意思是,如果用户将光标直接移动到文本框的中间并键入任何单词,那么我想在 space 按键事件中获取该单词;我想要那个词的位置并且可以以编程方式更改该词并更新文本框。 例如,如果用户键入

H!! my name vanani

但随后用户将光标直接移动到 'name' 之后并键入 'is sohan'

H!! my name is sohan

然后我想在文本框的按键事件中获取 'is' 的单词和位置以及 'sohan' 的相同位置。我需要用另一个词替换那个词的位置,并用新替换的文本更新文本框。

我看到了这些问题。 winforms - get last word.. and C# how to get latest char.. 但他们没有帮助我。请帮助我。

像这样:

if (Regex.IsMatch(textBox_string, @"\S*(?=\s?$)"))
{
    Match match = Regex.Match(textBox_string, @"\S*(?=\s?$)");
    string word = match.Value;
    int startingIndex = match.Index;
    int length = word.Length;
}

我找到了我的问题的答案。 这是为我工作的代码。

Bool isFirst = false;
int mytempindex;
 private async void mytxt_KeyUp_1(object sender, KeyRoutedEventArgs e)
    {  
      if (e.Key == Windows.System.VirtualKey.Space)
        {
          int i = mytxt.SelectionStart;
          if (i < mytxt.Text.Length)
          {
            if (isfirst == false)
             { 
                mytempindex = mytxt.SelectionStart;
                isfirst = true;
             }
            else
             {
                int mycurrent_index = mytxt.SelectionStart;
                        int templength_index = mycurrent_index - mytempindex;
                string word = mytxt.Text.Substring(mytempindex, templength_index); //It is the latest entered word.
               //work with your last word.
             }
          }
       }
   }

我不认为它适用于所有情况,但通过这个你可以了解如何从 Textbox 或 RichTextbox 获取最新输入的单词。