iphone學習筆記--NSString
// 聲明一個NSString<br />NSString *str;<br />// 使用stringWithFormat產生一格式化字串<br />str = [NSString stringWithFormat:@"This is %@","John"];<br />NSLog(@"str--->%@",str);<br />// 字串長度length;<br />NSLog(@"The length of this string is %@",[str length]);<br />// 字串比較 isEqualToString, 返回NO(false),isEqualToString區分大小寫<br />BOOL isequal = [str isEqualToString:@"this is John"];</p><p>// 字串序列比列 compare,返回結果NSComparisonResult<br />// type enum _NSComparisonResult{<br />// NSOrderedAscending = -1,<br />// NSOrderedSame,<br />// NSOrderedDescending<br /> // }<br />int result = [@"bool" compare:@"cool"];<br />NSLog(@"The result is %d",result);</p><p>// compare 比較規則options<br />// NSLiteralSearch 區分大小寫(完全比較)<br />// NSCaseInsensitiveSearch 不區分大小寫<br />// NSNumericSearch 只比較字串的個數,而不比較字串的字面值<br />int result1 = [@"This is John" compare:@"this is John" options:NSCaseInsensitiveSearch | NSNumericSearch];<br />NSLog(@"The result is %d",result1);</p><p>// 字串開頭是否包括另一字串 hasPrefix,返回結果YES(true)<br />BOOL isHas = [str hasPrefix:@"This"];<br />// 字串結尾是否包括另一字串 hasSuffix,返回結果YES(true)<br />BOOL isHas = [str hasSuffix:@"John"];</p><p>// 尋找字串在另一字串中的位置<br />NSRange range = [str rangeOfString:@"is" options:NSCaseInsensitiveSearch];<br />NSLog(@"The location in the string named 'str' of 'is' is @d",range.location);<br />