I’m using myField.CharacterPosition(x, y) to find what character I clicked on and then 2 loops to identify the the word defined by when I hit a space. The last word has no space after it how do I know I clicked on the last word in a TextArea ?
You could do something like
var myWord as String = TextArea.Text.Right(Your Last Word Length)
And if it matches your word then you have the last
Are you sure?
Seems to work for me…
But as we know every case can be different.
How do I know the last word length? I suppose I could split the whole text area into an array of words?
Without looking at the code (as I’m not at my computer) what if the last word is not unique? Eg
“There was the silent alarm. So he raised the alarm.”
Text.Right(num) only grabs the last number of characters of the string it doesn’t search for previous…
If you know the position of the last space, then, sudo code, TextArea.Text.Length - Last Space Position + 1, should give you the length of the word. Or something like that code.
Or as you suggest splitting the text into an Array of words, then just grab the last one…
Yes, but if you are over the third word “alarm” then that also matches the last word without being the last word.
So I don’t think just “matching the last word” will work.
In your forward loop to search the end of the clicked word, check the loop value (position) against the TextArea Len ?
Not only look for a space, but also look to see if you’ve reached the len of the text area. If you’ve reached the end, without encountering a space in the process, is this not the last word.
Or is the last word, word?
Since it sounds like this is happening based on a user action (i.e. click) and not in an iterative process where you may be looping over a large recordset or whatever, optimizing for speed is less important than code readability. Given your simple definition of a “word” being defined as a blank on either side, one simple approach would be to set a variable to the text area plus a space. Then search it instead of the text area itself. Or maybe prepend a space as well to avoid having to check the start index too:
var searchText as String = " " + TextArea1.Text + " "
Depending on why you are doing this, a check for only a blank may or may not give the expected results. What if the “word” has punctuation? Should a trailing period or comma or question mark or whatever be part of the “word”? I’m not asking you to answer the question for the sake of the forum; just to consider if you need a more robust definition of what delimits a “word”.
You may want to consider creating a regular expression with the delimiter characters ahead of the loop, and then checking for a match within the loop instead of comparing to a blank.