IOS NSString 用法详解

//NSString 操作均不改变自身值  
//构建字符串  
NSString *szTmp = @"A string";      //直接赋值  
szTmp = nil;  
  
int n = 5;  
NSString *szMyString = [NSString stringWithFormat:@"The number is %d",n];   //The number is 5  
[szMyString stringByAppendingFormat:@"%d",22];  //附加字符串返回值:The number is 522  
                                                //但是szMyString本身并没有改变,其值依然:The number is 5    
[cpp] view plaincopy
//长度与索引字符  
NSLog(@"%d",szMyString.length);                 //字符串长度:15  
NSLog(@"%c",[szMyString characterAtIndex:2]);   //返回字符:e  
[cpp] view plaincopy
//与c字符串相互转换  
printf("%s\n",[szMyString UTF8String]);         //转为__strong const char *  
const char *szTmp1 = [szMyString cStringUsingEncoding:NSUTF8StringEncoding];  
printf("%s\n",szTmp1);                          //转为__strong const char *  
  
NSLog(@"%@",[NSString stringWithCString:szTmp1 encoding:NSUTF8StringEncoding]); //转为nsstring  
[cpp] view plaincopy
//字符串写文件  
NSError *error;  
NSString *szPath = [NSHomeDirectory()           //应用程序沙盒路径  
                    stringByAppendingPathComponent:@"Documents/testFile.txt"];  //附加路径地址  
if (![szMyString writeToFile:szPath atomically:YES  //atomically:是否是原子访问文件的  
                    encoding:NSUTF8StringEncoding error:&error]) {          //写入成功返回yes 否则no  
    NSLog(@"Error writing to file :%@",[error localizedDescription]);       //输出错误描述  
    return 1;  
}  
NSLog(@"File write success");  
[cpp] view plaincopy
//文件读字符串  
NSString *szInString = [NSString stringWithContentsOfFile:szPath            //读取文件信息  
                        encoding:NSUTF8StringEncoding error:&error];  
if (!szInString)  
{  
    //失败  
}  
NSLog(@"%@",szInString);        //成功  
[cpp] view plaincopy
//字符串转为数组  
NSArray *arrayWord = [szMyString componentsSeparatedByString:@" "]; //有空格的拆分为单词保存  
NSLog(@"%@",arrayWord);  
[cpp] view plaincopy
//索引子串  
NSString *szSub1 = [szMyString substringToIndex:3];     //0-2,前3个:The  
NSLog(@"%@",szSub1);  
  
NSString *szSub2 = [szMyString substringFromIndex:4];   //4-尾,去掉前4个:number is 5  
NSLog(@"%@",szSub2);  
[cpp] view plaincopy
//范围索引  
NSRange range;  
range.location = 4;     //从4开始  
range.length = 6;       //6个字符  
NSString *szSub3 = [szMyString substringWithRange:range];       //number  
NSLog(@"%@",szSub3);  
[cpp] view plaincopy
//搜索与替换  
NSRange rangeSearch = [szMyString rangeOfString:@"is 5"];   //搜索  
if (rangeSearch.location != NSNotFound) {           //搜索不到是 NSNotFound  
    //成功:rangeSearch.location;//位置 rangeSearch.length;//长度  
}  
  
NSLog(@"%@",[szMyString stringByReplacingCharactersInRange:rangeSearch      //用位置匹配替换  
                                                withString:@"isn't 10"]);  
  
NSString *szReplaced = [szMyString stringByReplacingOccurrencesOfString:@" " withString:@"*"];  //匹配字符串替换  
NSLog(@"%@",szReplaced);  
[cpp] view plaincopy
//改变大小写  
NSLog(@"%@",[szMyString uppercaseString]);      //大写  
NSLog(@"%@",[szMyString lowercaseString]);      //小写  
NSLog(@"%@",[szMyString capitalizedString]);    //首字母大写  
[cpp] view plaincopy
//比较字符串  
NSString *sz1 = @"Hello World!";  
NSString *sz2 = @"Hello Mom!";  
if ([sz1 isEqualToString:sz2]) {/*相等*/}  
if ([sz1 hasPrefix:@"Hello"]) {NSLog(@"前部分相等");}        //从头开始比较  
if ([sz1 hasSuffix:@"d!"]) {NSLog(@"后部分相等");}       //从尾部比较  
[cpp] view plaincopy
//字符串转换数字  
NSString *szNumber = @"3.14";  
[szNumber intValue];  
[szNumber boolValue];  
[szNumber floatValue];  
[szNumber doubleValue];  
[cpp] view plaincopy
//可变字符串  
NSMutableString *szMuMyString = [NSMutableString stringWithString:@"Hello"];  
[szMuMyString appendFormat:@"World"];       //字符串,改变自身  
[szMuMyString uppercaseString];  
NSLog(@"%@",szMuMyString);  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值