zoukankan      html  css  js  c++  java
  • IOS NSString 用法详解

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