Search and Comparison Options - NSWidthInsensitiveSearch
These values represent the options available to many of the string classes’ search and comparison methods.
enum {
NSCaseInsensitiveSearch = 1,
NSLiteralSearch = 2,
NSBackwardsSearch = 4,
NSAnchoredSearch = 8,
NSNumericSearch = 64,
NSDiacriticInsensitiveSearch = 128,
NSWidthInsensitiveSearch = 256,
NSForcedOrderingSearch = 512,
NSRegularExpressionSearch = 1024
};
Constants
NSCaseInsensitiveSearch- A case-insensitive search.
NSLiteralSearch- Exact character-by-character equivalence.
NSBackwardsSearch- Search from end of source string.[NSString NSWidthInsensitiveSearch]
NSAnchoredSearch- Search is limited to start (or end, if
NSBackwardsSearch) of source string. NSNumericSearch- Numbers within strings are compared using numeric value, that is,
Name2.txt<Name7.txt<Name25.txt.This option only applies to compare methods, not find. NSDiacriticInsensitiveSearch- Search ignores diacritic marks.[NSString NSWidthInsensitiveSearch]For example, ‘ö’ is equal to ‘o’.
NSWidthInsensitiveSearch- Search ignores width differences in characters that have full-width and half-width forms, as occurs in East Asian character sets.For example, with this option, the full-width Latin small letter 'a' (Unicode code point U+FF41) is equal to the basic Latin small letter 'a' (Unicode code point U+0061).
NSForcedOrderingSearch- Comparisons are forced to return either
NSOrderedAscendingorNSOrderedDescendingif the strings are equivalent but not strictly equal.This option gives stability when sorting. For example, “aaa” is greater than "AAA” ifNSCaseInsensitiveSearchis specified. NSRegularExpressionSearch- The search string is treated as an ICU-compatible regular expression. If set, no other options can apply except
NSCaseInsensitiveSearchandNSAnchoredSearch. You can use this option only with therangeOfString:...methods andstringByReplacingOccurrencesOfString:withString:options:range:.
Discussion of [NSString NSWidthInsensitiveSearch]
See “Searching, Comparing, and Sorting Strings” for details on the effects of these options.
Example of [NSString NSWidthInsensitiveSearch]
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.filteredListContent removeAllObjects];
for (Notice *notice in allNoticeArray)
{
if ([scope isEqualToString:@"标题"])
{
NSComparisonResult result = [notice.title compare:searchText
options:NSWidthInsensitiveSearch
range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[self.filteredListContent addObject:notice];
}
}
}
}
Example of [NSString NSWidthInsensitiveSearch]
NSString *fold1 = [str1 stringByFoldingWithOptions:NSDiacriticInsensitiveSearch | NSCaseInsensitiveSearch | NSWidthInsensitiveSearch locale:[NSLocale currentLocale]];
NSString *fold2 = [str2 stringByFoldingWithOptions:NSDiacriticInsensitiveSearch | NSCaseInsensitiveSearch | NSWidthInsensitiveSearch locale:[NSLocale currentLocale]];
NSRange range = [fold1 rangeOfString:fold2]; // or other string comparisons
Example of [NSString NSWidthInsensitiveSearch]
static NSInteger comparator(id a, id b, void* context)
{
NSInteger options = NSCaseInsensitiveSearch
| NSNumericSearch // Numbers are compared using numeric value
| NSDiacriticInsensitiveSearch // Ignores diacritics (â == á == a)
| NSWidthInsensitiveSearch; // Unicode special width is ignored
return [(NSString*)a compare:b options:options];
}