//首先说下NSString的初始化方法
NSString *str1 = @"123";
NSString *str2 = @"123";
//注意上面两个字符串对象str1,str2都是指向同一块内存空间的,也就是@“123”所在空间
NSLog(@"%p == %p",str1,str2);
//运行结果如下:
2015-08-01 10:16:46.215 随便建的练手用[1157:13413] 0x100001038 == 0x100001038
//实例化一个NSString对象,当前对象的值为空
NSString *str3 = [[NSString alloc] init];
//类方法创建字符
NSString *str4 = [NSString stringWithFormat:@"%@在写%@",@"某人",@"博客"];
//对应的实例方法
NSString *str5 = [[[NSString alloc] init] initWithFormat:@"%@在写%@",@"某人",@"博客"];
//通过已有的字符串生成一个新的字符串
NSString *str6 = [[[NSString alloc] init] initWithString:@"str4"];
//通过对一个data进行解码得到一个新的字符串
NSString *str7 = [[[NSString alloc] init] initWithData:(NSData *)encoding:(NSStringEncoding)];
//NSString常用方法
//1、获取字符串长度
NSUInteger stringLength1 = str4.length;
NSUInteger stringLength2 = [str4 length];
//2、字符串的比较
//比较字符串的指针可以用 == 来直接进行比较
//比较两个字符串的内容
BOOL isSame = [str4 isEqualToString:str5];
//比较字符串大小
int stringCompare = [str4 compare:str5];
//这里返回值NSComparisonResult是个枚举类型,官方文档定义如下: typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending};
//比较字符串大小时加一些额外的控制选项
NSComparisonResult stringCompare2 = [str4 compare:str5 options:(NSStringCompareOptions)];
//NSStringCompareOptions 也是一个枚举类,官方文档定义如下:
typedef NS_OPTIONS(NSUInteger, NSStringCompareOptions) {
NSCaseInsensitiveSearch = 1,
NSLiteralSearch = 2, /* Exact character-by-character equivalence */
NSBackwardsSearch = 4, /* Search from end of source string */
NSAnchoredSearch = 8, /* Search is limited to start (or end, if NSBackwardsSearch) of source string */
NSNumericSearch = 64, /* Added in 10.2; Numbers within strings are compared using numeric value, that is, Foo2.txt < Foo7.txt < Foo25.txt; only applies to compare methods, not find */
NSDiacriticInsensitiveSearch NS_ENUM_AVAILABLE(10_5, 2_0) = 128, /* If specified, ignores diacritics (o-umlaut == o) */
NSWidthInsensitiveSearch NS_ENUM_AVAILABLE(10_5, 2_0) = 256, /* If specified, ignores width differences (‘a’ == UFF41) */
NSForcedOrderingSearch NS_ENUM_AVAILABLE(10_5, 2_0) = 512, /* If specified, comparisons are forced to return either NSOrderedAscending or NSOrderedDescending if the strings are equivalent but not strictly equal, for stability when sorting (e.g. “aaa” > “AAA” with NSCaseInsensitiveSearch specified) */
NSRegularExpressionSearch NS_ENUM_AVAILABLE(10_7, 3_2) = 1024 /* Applies to rangeOfString:…, stringByReplacingOccurrencesOfString:…, and replaceOccurrencesOfString:… methods only; the search string is treated as an ICU-compatible regular expression; if set, no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch */
};
//字符串拼接
NSString *str8 = [str4 stringByAppendingString:str5];
//可以按照一定格式传入
NSString *str9 = [str4 stringByAppendingFormat:@"%@123",str5];
//字符串查找及截取
//是否以123开头
BOOL hasPrefix = [str4 hasPrefix:@"123"];
//是否以123结尾
BOOL hasSuffix = [str4 hasSuffix:@"123"];
//得到字符串str4 索引号为 3 的字符
unichar c = [str4 characterAtIndex:3];
//从索引号为1开始截取到最后,包括索引号为1的字符
NSString * str10 = [str4 substringFromIndex:1];
//从头开始截取到索引号为3的字符,不包括索引号为3的字符
NSString *str11 = [str4 substringToIndex:3];
//根据range的范围进行截取
NSString *str12 = [str4 substringWithRange:NSMakeRange(1, 2)];
//字符串大小写转换
//将小写改成大写
NSString *newString1 = [@"afjasdhj" uppercaseString];
//将大写改成小写
NSString *newString2 = [@"FDSHFJAH" lowercaseString];
//将首个字符改成大写,其余都小写
NSString *newString3 = [@"adGJGHsfad" capitalizedString];
//字符串中的替换函数
//将 写 字 替换为 读
NSString *str13 = [str4 stringByReplacingOccurrencesOfString:@"写" withString:@"读"];
//将range范围内的字符置换为zsz
NSString *str14 = [str4 stringByReplacingCharactersInRange:NSMakeRange(1, 2) withString:@"zsz"];
//通过 在 将字符串进行分割成 n 个对象然后存入数组
NSArray *array = [str4 componentsSeparatedByString:@"在"];
//可变字符串
//初始化方法:因为可变字符串为不可变字符串的子类,因此不可变字符串的方法可变字符串都适用,以下是初始化创建一个长度为10 的字符串,这是可以改变的
NSMutableString *mutString1 = [NSMutableString stringWithCapacity:10];
//在可变字符串上修改内容
[mutString1 appendString:@"0123"];
[mutString1 appendFormat:@"%@45456",@"123"];
//删除字符的方法
//删除字符串mutString1中range范围中的字符
[mutString1 deleteCharactersInRange:NSMakeRange( 1, 3)];