note : 1.8 Allocating and Making Use of Strings

#NSString && NSMutableString
The NSString  class is immutable, meaning that once it is created, its contents cannot be modified. Mutable strings represented with the NSMutableString can be modified once they are created.  
这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重新给这个字符串赋值。而NSMutableString 创建赋值以后可以动态在该字符串上更改内容与长度。
 

##Objective-C strings 
//字符串表示
Objective-C strings should be placed inside double quotes. The starting double-quote should be prefixed with an at sign (@). For instance: 

@"Hello world"

//格式化输出
You can use the %s format specifier to print a C String out to the console. In contrast, use the %@ format specifier to print out NSString objects. For instance:

NSLog(@"mutableString = %@", mutableString);

##NSRange && rangeOfString:options

/* 查找My Simple String中的simple忽略大小写 */ 
NSString *haystack = @"My Simple String";
NSString *needle = @"simple";
NSRange range = [haystack rangeOfString:needle
    options:NSCaseInsensitiveSearch];
if (range.location == NSNotFound){
    /* Could NOT find needle in haystack */
} else {
    /* Found the needle in the haystack */
    NSLog(@"Found %@ in %@ at location %lu",
    needle,
    haystack,
    (unsigned long)range.location);
}
/* 替换My MacBook Pro 为 MacBook Pro,注意空字符串的替换[NSString string]*/
NSMutableString *mutableString =
    [[NSMutableString alloc] initWithString:@"My MacBook"];
/* Add string to the end of this string */
[mutableString appendString:@" Pro"];
/* Remove the "My " string from the string */
[mutableString 
    replaceOccurrencesOfString:@"My "
    withString:[NSString string] /* Empty string */
    options:NSCaseInsensitiveSearch /* Case-insensitive */
    range:NSMakeRange(0, [mutableString length])]; /* All to the end */
NSLog(@"mutableString = %@", mutableString); /* mutableString = MacBook Pro */

更多扩展可浏览:

  1. http://blog.csdn.net/xys289187120/article/details/6777283
  2. http://blog.csdn.net/xys289187120/article/details/6778453

你可能感兴趣的:(ios,Objective-C)