Foundation框架学习-NSString

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Foundation框架学习-NSString相关的知识,希望对你有一定的参考价值。

点击查看代码

技术分享
 1     char *s = "Hello World";
 2     NSString *str = @"Hello Warcraft";
 3     //C -> OC
 4     NSString *str1 = [NSString stringWithUTF8String: s];
 5     NSLog(@"str1 = %@", str1); //=> Hello World
 6     //OC -> C
 7     NSLog(@"s1 = %s", [str UTF8String]); //=> Hello Warcraft
 8 
 9     NSString *str3 = @"ios"; //自动释放内存
10     NSString *str4 = [[NSString alloc] init]; //手动释放内存
11 
12     //格式化字符串
13     int a = 10;
14     int b = 90;
15     NSString *str5 = [NSString stringWithFormat: @"%d, %d", a, b];
16     NSLog(@"str5 = %@", str5); //=> 10, 20
17 
18     //拼接字符串
19     NSString *str6 = [str5 stringByAppendingString: str3];
20     NSLog(@"str6 = %@", str6); //=> 10, 20iOS
21 
22     //大小写转换
23     NSString *str7 = @"aBcDeF";
24     //转换成小写
25     NSString *str8 = [str7 lowercaseString];
26     NSLog(@"str8 = %@", str8); //=> abcdef
27     //转化成大写
28     NSString *str9 = [str7 uppercaseString];
29     NSLog(@"str9 = %@", str9); //=> ABCDEF
30 
31     NSString *website = @"www.cnblogs.com";
32     //判断前缀
33     BOOL hasPrefix = [website hasPrefix: @"www."]; //整个website字符串也可以参加匹配,返回YES
34     if(hasPrefix){
35         NSLog(@"有对应前缀"); //=> 有对应前缀
36     }else{
37         NSLog(@"没有对应前缀");
38     }
39     //判断后缀
40     BOOL hasSuffix = [website hasSuffix:@"帅爆了"];
41     if(hasSuffix){
42         NSLog(@"有对应后缀"); //=> 有对应后缀
43     }else{
44         NSLog(@"没有对应后缀");
45     }
46 
47     //判断两个字符串是否相同
48     NSString *str10 = @"hello";
49     NSString *str11 = @"hello";
50     if([str10 isEqualToString: str11]){
51         NSLog(@"两个字符串相同"); //Right
52     }else{
53         NSLog(@"两个字符串不相同");
54     }
55     //分割
56     //按照制定字符分割字符串
57     NSString *str12 = @"a,b,c,d,e,f";
58     NSArray *strArray = [str12 componentsSeparatedByString:@","];
59     for(NSString *str in strArray)
60     {
61         NSLog(@"str = %@", str);
62     }    
63     /*
64     *str = a
65     *str = b
66     *str = c
67     *str = d
68     *str = e
69     *str = f
70     */
NSString(一)

以上是关于Foundation框架学习-NSString的主要内容,如果未能解决你的问题,请参考以下文章

Foundation框架之 NSString 和 集合

Foundation框架之 NSString 和 集合

Foundation框架系列-NSString

Foundation框架字符串对象

OC Foundation框架 字符串

oc67--NSString1