Get the last word entered in the text box of Windows phone 8

I am developing one application for Windows. In my application, I want to get the last word entered into the text field not the last word. And I want to change the last entered word to the space bar. I get the last word in a key event like this:

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(); } } } 

I get the last word with this method, but really want to get the last word entered by the user. Value, if the user moves the cursor directly to the middle of the text field and types any word, then I want to get this word in the pressed space bar; I want the position of this word and can change this word programmatically and update the text box. For example, if the user enters

H !! my name is vanani

but then the user moves the cursor immediately after the "name" and the types are ishan

H !! my name is sohan

then I want to get the word and position "is" and also for "sohan" in the key event of the text field. I need a position to replace this word with another word and update the text box with new new text.

I saw these questions. winforms - get the last word .. and C # how to get the last char .. but they didn’t "I will help me. Please help me.

+1
string c # regex textbox windows-phone-8
source share
2 answers

I based the answer to my question. Here the code worked for me.

 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. } } } } 

I do not think that it works in all situations, but through this you can get an idea of ​​how to get the last word entered from a text field or RichTextbox.

0
source share

Like this:

 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; } 
0
source share

All Articles