1.直接创建方式:
NSString *str = @"abc";
2. 格式化创建:
NSString *st2 = [NSString stringWithFormat:@"%@", @"jack"];
NSString *str = [[NSString alloc] initWithFormat:@"My age is %d", 10];
3.文本编码:
中文 :GBK(3个字节) GB2312(3个字节)
UTF - 8:国际通用编码(2个字节)
4. 字符串写入文件:
[str writeToFile:@"/Users/zhaoxiaohu/Desktop/" atomically:YES encoding:NSUTF8StringEncoding error:&err];
5.字符串从文件读取:
NSString *str = [NSString stringWithContentsOfFile:@"/Users/zhaoxiaohu/Desktop/str.txt" encoding:NSUTF8StringEncoding error:&err];
6.字符串比较函数:
NSComparisonResult result = [str1 compare:str2 options:NSCaseInsensitiveSearch|NSNumericSearch];
返回值:NSOrderedAscending(str1<str2)
NSOrderedDescending(str1>str2)
NSOrderedSame(str1 = str2)
7. 判读字符串是否相等:
[str1 isEqualToString:str3]
8. 检测字符串前后缀:
[url hasPrefix:@"http://"]; 字符串是否以http://开头
[imgName hasSuffix:@".jpg"]; 检测字符串是否以.jpg结尾
9.截取字符串:
NSRange range = [str1 rangeofString:str2]; //str1 中找str2 |
|