在 iOS 开发中 NSString 字符串对象是使用的最多的,总结一下方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
-(void) testNSString{ int i = 10; NSString *str = [NSString stringWithFormat:@"%d",i]; //字符串格式化 NSLog(@"%@",str); str = @"sysprogram 2017"; NSLog(@"%@",str); NSString *strRet = [str substringToIndex:7];//截取掉下标7之后的字符串 NSLog(@"%@",strRet); strRet = [str substringFromIndex:2]; //截取掉下标2之前的字符串 NSLog(@"%@",strRet); strRet = [str substringFromIndex:str.length- 2]; //只获取后面的两位字符串 NSLog(@"%@",strRet); //设置截取字符串的范围,从第二位到第七位 NSRange rang = NSMakeRange(2, 7); NSString * strRang = [str substringWithRange:rang]; NSLog(@"rang = %@",strRang); strRet = [str capitalizedString]; //首字母大写 NSLog(@"首字母大写:%@",strRet); strRet = [str uppercaseString]; //全部转为大写 NSLog(@"全部大写:%@",strRet); strRet = [str lowercaseString]; //全部转为小写 NSLog(@"全部小写:%@", strRet); strRet = [str stringByAppendingString:@"xxx"]; //字符串相加 NSLog(@"%@",strRet); //查找字符串 rang = [str rangeOfString:@"2017"]; NSLog(@"查找的字符串 index 为 %lu", (unsigned long)rang.location); //将搜索中的字符串替换成为一个新的字符串 strRet = [str stringByReplacingCharactersInRange:rang withString:@"1999"]; NSLog(@"替换后字符串为%@", strRet); //将 2017 字符串替换成 1998 strRet = [str stringByReplacingOccurrencesOfString:@"2017" withString:@"1998"]; NSLog(@"替换后字符串为%@", strRet); //字符串比较区分大小写 if ([str isEqualToString:@"sysprogram 2017"]) { NSLog(@"字符串完全相等"); } //字符串开头比较 if([str hasPrefix:@"sys"]) { NSLog(@"字符串结尾为 sys"); } //字符串结尾比较 if([str hasSuffix:@"2017"]) { NSLog(@"字符串结尾为 2017"); } } |
与文件目录字符串处理相关函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
//获取文件所在路径 NSString *strFile = @"/Users/exchen/test.txt"; NSString *strPath = [strFile stringByDeletingLastPathComponent]; NSLog(@"strPath: %@",strPath); //strPath2: /Users/exchen //删除文件的扩展名 NSString *strPath2 = [strFile stringByDeletingPathExtension]; NSLog(@"strPath2: %@",strPath2); //strPath: /Users/exchen/test //获取文件的扩展名 NSString *strExtension = [strFile pathExtension]; NSLog(@"extension: %@",strExtension); //extension: txt //获取文件名 NSString *strName = [strFile lastPathComponent]; NSLog(@"strName %@",strName); //strName test.txt //路径相加 NSString *strNewFilePath = [strFile stringByAppendingPathComponent:@"test2.txt"]; NSLog(@"strNewFilePath: %@",strNewFilePath); //strNewFilePath: /Users/exchen/test.txt/test2.txt //获取该路径的所有组成部分 NSArray *strPaths = [strFile pathComponents]; NSLog(@"strPaths: %@",strPaths); /*strPaths: ( "/", Users, exchen, "test.txt" ) */ |
NSString 和 NSData 相互转换
1 2 3 4 5 6 7 8 9 10 |
NSData* data = [@"string" dataUsingEncoding:NSUTF8StringEncoding]; NSData *data; NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSData *data; char *test=[data bytes]; byte* tempData = malloc(sizeof(byte)*16); NSData *content=[NSData dataWithBytes:tempData length:16]; |
还有更多的方法,待续......
转载请注明:exchen's blog » NSString 使用方法总结