您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iPhone 開發:搜尋字串位置與判斷是否存在

編輯:iOS 開發綜合

搜尋字串位置

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 參數使用。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved