Objective-C 中可用 rangeOfString 搜尋子字串。回傳的 NSRange 包含 location 與 length,若找不到,location 會等於 NSNotFound。
NSString *str = @"1234567890";
NSRange r = [str rangeOfString:textview.text];
if (r.location != NSNotFound) {
NSLog(@"found at location = %d,length = %d", r.location, r.length);
} else {
NSLog(@"not found");
}
這種寫法常用於輸入驗證、關鍵字定位或簡易文字處理。若需要忽略大小寫,也可搭配 options 參數使用。