Searching for whole words
Searching for whole words
- Subject: Searching for whole words
- From: Manfred Lippert <email@hidden>
- Date: Thu, 30 May 2002 22:32:54 +0200
Hi,
I have a search panel with a "Whole Words" search option.
For searching "Whole Words" in an NSAttributedString, I am using the
selector "nextWordFromIndex:forward:". I found out that backwards searching
finds the beginning of words, but forward searching always finds the _end_
of a word (and _not_ the beginning of the next word).
My question: Can I rely on this behaviour? Is this somewhere documented?
Here is my code to check if the NSRange range points to a "whole word" in
the NSAttributedString string:
if ([string nextWordFromIndex:(range.location + 1)
forward:NO] == range.location &&
[string nextWordFromIndex:(range.location + range.length - 1)
forward:YES] == range.location + range.length)) {
// Yes, "range" points to a "whole word"
} else {
// No, "range" does not point to a "whole word"
}
As you can see, this is relying on the fact that
nextWordFromIndex:forward:YES always finds the end of a word, and not the
beginning of the next word.
Is this OK? Or are there any better search algorithms for finding whole
words? (TextEdit's search panel has no "Whole Words" option, so I can
compare with its example code.)
Thanks,
Mani
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.