+ 3

remove the next word in a string in C#?

I want to remove the next word from the text (next, after the position of the caret), but it returns an unhandled exception..any help? private void deleteToolStripMenuItem_Click(object sender, EventArgs e) { int position = TextArea.SelectionStart; //get position of caret int MaxLength = TextArea.Text.Length; //get textbox's text length String ForwardString = TextArea.Text.Substring(position, MaxLength); //filter from caret to the end of the text bool MetChar = false; // when you meet a character that is not whitespace you get allowed to break when you meet a whitespace // remove characters from the string until you meet a whitespace that ends the word foreach (char ConsecutiveChar in ForwardString) { if (ConsecutiveChar == ' ' && MetChar == false) { ForwardString.Substring(1, ForwardString.Length); } else if (ConsecutiveChar == ' ' && MetChar == true) { break; } else {if (MetChar == false) { MetChar = true; ForwardString.Substring(1, ForwardString.Length); }} } }

20th Nov 2018, 6:01 PM
Stanislav Vladev
Stanislav Vladev - avatar
4 Réponses
+ 2
20th Nov 2018, 11:03 PM
sneeze
sneeze - avatar
+ 1
Please elaborate more. I can see you copy a string : Forwardstring but it is unclear to me what you want to do ? Which word needs to be remove The second word of the string ?
20th Nov 2018, 9:21 PM
sneeze
sneeze - avatar
+ 1
if the beginning of the string begins with whitespace then remove characters until you meet a character. then begin to remove characters until you meet a whitespace and then break and return the modified string.
20th Nov 2018, 9:54 PM
Stanislav Vladev
Stanislav Vladev - avatar
0
this doesnt involve the position from the caret but thanks
20th Nov 2018, 11:10 PM
Stanislav Vladev
Stanislav Vladev - avatar