NSString是NSObject的一个子类,不同于标准的ANSI的字符串,下面大致看看这个类怎么使用。
首先安装GNUStep编译环境
编译链接代码的方式如下:
$ gcc -o test test.m -I/GNUstep/System/Library/Headers -fconstant-string-class=NSConstantString -L/GNUstep/System/Library/Libraries -lobjc -lgnustep-base
1,创建和初始化字符串
#import <Foundation/Foundation.h> int main (int argc, const char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //创建一个空字符串 NSString *emptyStr = [NSString string]; NSLog(@"%@",emptyStr); /* NSString *emptyStr2 = [[NSString alloc] init]; NSLog(@"%@",emptyStr2); [emptyStr2 release]; */ //用Byte[]数组初始化字符串 byte bdata[4] = {'a',66,'c','d'}; NSString *bStr = [NSString alloc]; bStr = [bStr initWithBytes:bdata length:4 encoding:NSASCIIStringEncoding]; NSLog(@"%@",bStr); [bStr release]; //用unichar[]数组初始化字符串(unichar 是两个字节长的char,代表unicode的一个字符) unichar ucdata[4] = {'e','f','g','h'}; NSString *ucStr = [NSString alloc]; ucStr = [ucStr initWithCharacters:ucdata length:4]; NSLog(@"%@",ucStr); [ucStr release]; //用Char[]数组初始化字符串 char cdata[5] = "abcd"; NSString *cStr = [NSString alloc]; cStr = [cStr initWithCString:cdata length:4]; NSLog(@"%@",cStr); [cStr release]; //格式化字符串 NSString *fStr = [[NSString alloc] initWithFormat:@"Hello ciaos"]; NSLog(@"%@",fStr); [fStr release]; //用字符串初始化字符串 NSString *sStr = [NSString stringWithFormat:@"%s am %d years now", "I" , 25, nil]; NSLog(@"%@",sStr); //... [pool drain]; return 0; }2,从文件读取并创建和初始化字符串
#import <Foundation/Foundation.h> int main (int argc, const char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //读取文件内容 NSString *fStr = [NSString stringWithContentsOfFile:@"a.txt" encoding:NSASCIIStringEncoding error:NULL]; NSLog(@"%@",fStr); //或者 NSString *sStr = [[NSString alloc] initWithContentsOfFile:@"a.txt" encoding:NSASCIIStringEncoding error:NULL]; NSLog(@"%@",sStr); [sStr release]; [pool drain]; return 0; }
3,从网页读取文本并用来创建和初始化字符串
#import <Foundation/Foundation.h> int main (int argc, const char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //读取网页内容 NSStringEncoding encoding; NSError *err = nil; NSString *uStr = [[NSString alloc] initWithContentsOfURL: [NSURL URLWithString:@"http://127.0.0.1/index.php"] usedEncoding:&encoding error:&err]; NSLog(@"%@",uStr); [uStr release]; //或者 NSString *uStr2 = [NSString stringWithContentsOfURL: [NSURL URLWithString:@"http://127.0.0.1/index.php"] usedEncoding:&encoding error:&err]; NSLog(@"%@",uStr2); [pool drain]; return 0; }4,将字符串写入文件或URL
#import <Foundation/Foundation.h> int main (int argc, const char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //将字符串写入文本文件 NSString *str = @"This should be written to file"; BOOL res = [str writeToFile:@"b.txt" atomically:YES encoding:NSUTF8StringEncoding error:NULL]; NSLog(@"%d",res); [pool drain]; return 0; }
5,获取字符串长度
#import <Foundation/Foundation.h> int main (int argc, const char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //字符串长度 NSString *str = @"length of string"; NSUInteger len = [str length]; NSLog(@"%d",len); //按照指定编码获取长度 unichar ucdata[4] = {'e','f','g','h'}; NSString *str2 = [NSString alloc]; str2 = [str2 initWithCharacters:ucdata length:4]; NSUInteger len2 = [str2 lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; NSLog(@"%d",len2); // 4 len2 = [str2 lengthOfBytesUsingEncoding:NSASCIIStringEncoding]; NSLog(@"%d",len2); // 8 [str2 release]; [pool drain]; return 0; }
6,获取字符和字节流
#import <Foundation/Foundation.h> int main (int argc, const char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //获取某个字符 NSString *str = @"Objective-C programming"; unichar c = [str characterAtIndex:2]; NSLog(@"%c",c); // 'j' //获取某范围字符串 unichar buf[10] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}; [str getCharacters:buf range:NSMakeRange(3,2)]; NSLog(@"%c%c%c%c%c%c%c%c",buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]); // "ec" [pool drain]; return 0; }
7,转换为标准ANSI字符串
#import <Foundation/Foundation.h> int main (int argc, const char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //获取ANSI标准字符串 NSString *str = @"Objective-C programming"; const char *res = [str cStringUsingEncoding:NSASCIIStringEncoding]; NSLog(@"%s",res); // "Objective-C programming" [pool drain]; return 0; }8,字符串拼接
#import <Foundation/Foundation.h> int main (int argc, const char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //按照指定格式拼接字符串 NSString *str = @"Objective-C programming"; str = [str stringByAppendingFormat:@" %s programming %s","2.0","is difficult",nil]; NSLog(@"%@",str); // "Objective-C programming" //拼接字符串 NSString *str1 = @"Hello ",*str2 = @"Ciaos"; str1 = [str1 stringByAppendingString:str2]; NSLog(@"%@",str1); // "Hello Ciaos" [pool drain]; return 0; }
9,字符串截取
#import <Foundation/Foundation.h> int main (int argc, const char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //字符串截取 NSString *str = @"Objective-C programming"; NSString *res = [str substringFromIndex:3]; NSLog(@"%@",res); // "ective-C programming" res = [str substringWithRange:NSMakeRange(3,4)]; NSLog(@"%@",res); // "ecti" res = [str substringToIndex:5]; NSLog(@"%@",res); // "Objec" [pool drain]; return 0; }10,字符串查找
#import <Foundation/Foundation.h> int main (int argc, const char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //字符串截取 NSString *str = @"Objective-C programming"; NSString *obj = @"prog"; NSRange range = [str rangeOfString: obj]; NSLog(@"%d %d",range.location,range.length); // 12 4 obj = @"prod"; range = [str rangeOfString: obj]; NSLog(@"%d %d",range.location,range.length); // 2147483647 0 找不到 [pool drain]; return 0; }
11,字符串切分
#import <Foundation/Foundation.h> int main (int argc, const char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //字符串切分 NSString *str = @"apple:pear:banana"; NSArray *arr = [str componentsSeparatedByString:@":"]; NSLog(@"%@",arr); [pool drain]; return 0; }
12,NSString 和 NSMutableString 使用区别
#import <Foundation/Foundation.h> int main (int argc, const char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *s1 = @"I am s1"; NSString *s2 = s1; s2 = @"I am s2"; NSLog(@"\ns1 = %@\ns2 = %@\n",s1,s2); // s1 = I am s1 // s2 = I am s2 NSLog(@"\naddr s1 = %p\naddr s2 = %p\n",s1,s2); // addr s1 = 0x402000 // addr s2 = 0x40200c not the same NSMutableString *ms1 = [NSMutableString stringWithString:@"I am ms1"]; NSMutableString *ms2 = ms1; [ms2 setString:@"I am ms2"]; NSLog(@"\ns1 = %@\ns2 = %@\n",ms1,ms2); // s1 = I am ms2 // s2 = I am ms2 NSLog(@"\naddr ms1 = %p\naddr ms2 = %p\n",ms1,ms2); // addr ms1 = 0x1a53738 // addr ms2 = 0x1a53738 the same [pool drain]; return 0; }其他——子串替换,是否包含指定前缀后缀,大小写转换等,NSString类提供了比较丰富的字符串操作函数。