NSString and NSMutableString常用方法

NSString and NSMutableString

use @"abc" to mean NSString
ex: NSString *str = @"Hello";

use content of file to create NSString
ex: NSString *str = [NSString stringWithContentsOfFile:@"/path/to/file"]

use c characters to create NSString
ex: char *cStr="hello";
NSString *str = [NSString stringWithCString: cStr]

get length of NSString
ex: unsigned int strLen = [str length]

append on NSString to another
ex: NSString *str = @"Hello";
NSString *str2 = [str stringByAppendingString: @"abc"]

append a format:
ex: NSString *str3 = [str2 stringByAppendingFormat: @"%d", 2003]

search for subString:
ex: NSRange loc = [str rangeOfString:@"The"]

what is NSRange:
typedef struct _NSRange{
unsigned int location;
unsigned int length;
}NSRange;

breaking a string into components:
ex: NSArray *fields = [str componentsSeperatedByString:@"abc"];

create NSMutableString from NSString:
ex: NSString *str = @"hello";
NSMutableString *ms = [NSMutableString stringWithString: str];
原文地址:https://www.cnblogs.com/SunWentao/p/1392275.html