首页 > 其他 > 详细

Objective-C字符串初识(下)

时间:2015-11-01 01:41:44      阅读:251      评论:0      收藏:0      [点我收藏+]

1.基本数据类型与字符串的相互转换

  1.1基本数据类型转换到NSString

 1         // 1 int -> 转换成NSString
 2         //  5   @"5"
 3         int age = 18;
 4         NSString * str = [[NSString alloc] initWithFormat:@"%d",age];
 5         
 6         NSLog(@"%@",str);
 7         
 8         
 9         /*
10          //7.将任意输入的数字,转换成字符串。
11          //如 125 ---》 @"125"      scanf
12          
13          */
14 //        {
15 //            int num;
16 //            scanf("%d",&num);
17 //            NSString * str = [[NSString alloc] initWithFormat:@"%d",num];
18 //            NSLog(@"%@",str);
19 //        }
20         // float -> NSString
21         float f = 1.25;
22         NSString * str2 = [[NSString alloc] initWithFormat:@"%f",f];
23         NSLog(@"%@",str2);
24         // double -> NSString
25         double d = 2.53456;
26         NSString * str3 =[[NSString alloc] initWithFormat:@"%f",d];
27         // char -> NSString
28         char c = a;
29         NSString * str4 = [[NSString alloc] initWithFormat:@"%c",c];

  1.2 NSStirng向基本数据类型转换

 1         // NSString -> int
 2         NSString * ageStr = @"18";
 3         int age1 = [ageStr intValue];
 4         
 5         NSLog(@"%d",age1);
 6         // NSString -> float
 7         NSString * str5 = @"1.23";
 8         float f1 = [str5 floatValue];
 9         NSLog(@"%f",f1);
10         // NSString -> double
11         NSString * str6 = @"2.345678";
12         double d1 = [str6 doubleValue];
13         NSLog(@"%.4f",d1);

  需要保证数据的正确性

1         NSString * str7 = @"i386";
2         int i2 = [str7 intValue];
3         NSLog(@"%d",i2);
4         //这种情况无法转换得到386

 

2.大小写的转换

  2.1 小写转化为大写

1         // 转换成大写
2         NSString * str10 = @"qianfeng";
3         NSString * str11 = [str10 uppercaseString];
4         NSLog(@"%@",str11);

  2.2 大写转化为小写

1         // 转换成小写
2         NSString * str12 = [str11 lowercaseString];
3         NSLog(@"%@",str12);

  2.3 首字母大写

1         // 首字母大写,其余字母小写
2         NSString * str13 = [str12 capitalizedString];
3         NSLog(@"%@",str13);
4         

 

3.字符串的重组

  3.1多数据拼接为一个字符串

    stringWithFormat方法

1         int year = 2015;
2         int month = 10;
3         int day = 29;
4         
5         NSString * address = @"北京市海淀区宝胜北里28号四楼前锋教育";
6         
7         NSString * dateAndAddress = [NSString stringWithFormat:@"%d年%d月%d日 %@",year,month,day,address];
8         
9         NSLog(@"%@",dateAndAddress);

  3.2 字符串末尾增加新字符串

    stringByAppendingString方法

1         NSString * str = @"qianfeng";
2         // qianfeng.com
3         NSString * str2 = [str stringByAppendingString:@".com"];
4         NSLog(@"%@",str2);

  3.3 在指定范围内替换字符

    stringByReplacingCharactersInRange方法

1         NSString * str3 = @"www.baidu.com";
2         // www.qianfeng.com
3         NSRange range = {4,5};
4         NSString * str4 = [str3 stringByReplacingCharactersInRange:range withString:@"qianfeng"];
5         NSLog(@"%@",str4);

  3.4 使用新字符串替换原有字符串

    stringByReplacingOccurrencesOfString:(NSString *)    withString:(NSString *) 方法

1         // www.qianfeng.com
2         // www-qianfeng-com
3         NSString * str5 = @"www.qianfeng.com";
4         NSString * str6 = [str5 stringByReplacingOccurrencesOfString:@"." withString:@"-"];
5         // occur
6         NSLog(@"%@",str6);

4. 字符串写入文件 与从文件读取字符串

  4.1 写入文件内

    - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;

 1        NSString * str = @"我在千锋快乐地学习";
 2         NSString * path = @"/Users/wangqitai/Desktop/1559 /day04/";
 3         
 4         NSError * a;
 5         
 6         [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&a];
 7         
 8         if (a == nil) {
 9             NSLog(@"写入成功 %@",a);
10         } else {
11             NSLog(@"写入失败 %@",a);
12         }

  

  4.2 从文件读入字符串

    - (nullable instancetype)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;

 1         NSString * path = @"/Users/wangqitai/Desktop/1559 /day04/str.txt";
 2         
 3         NSError * a;
 4         
 5         NSString * str = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&a];
 6         
 7         if (a == nil) {
 8             NSLog(@"读入成功 %@",str);
 9         } else {
10             NSLog(@"读入失败 %@",a);
11         }

 

5. 可变字符串

  5.1创建可变字符串

1         // 1 创建可变字符串
2         NSMutableString * muStr = [[NSMutableString alloc] initWithString:@"one"];
3         
4         NSMutableString * muStr2 = [[NSMutableString alloc] init];
5         
6         NSString * str = [[NSString alloc] init];
7         NSLog(@"before set:%@",muStr2);

  5.2 set方法

1         // 1 重置字符串
2         [muStr2 setString:@"abc"];
3         NSLog(@"%@",muStr2);
4         NSMutableString * muStr3 = @"two";
5         NSLog(@"%@",muStr3);

  5.3 追加字符串

    appendString   appendFormat

1         [muStr2 appendString:@"def"];
2         NSLog(@"%@",muStr2);
3 //        int count = 6;
4         [muStr2 appendFormat:@"是%lu个字符",[muStr2 length]];
5         
6         NSLog(@"%@",muStr2);

  5.4 插入字符串  

    在index处插入 

1             NSMutableString * muStr4 = [[NSMutableString alloc] initWithString:@"two"];
2             
3             [muStr4 insertString:@"abc" atIndex:1];
4             NSLog(@"%@",muStr4);

  5.5 删除字符串

    删除起始index为2 长度为2的字符串

1             NSRange range = {2,2};
2             [muStr4 deleteCharactersInRange:range];
3             NSLog(@"%@",muStr4);

 

Objective-C字符串初识(下)

原文:http://www.cnblogs.com/gwkiOS/p/4926604.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!