1 #import <Foundation/Foundation.h>
  2 
  3 int main(int argc, const char * argv[])
  4 {
  5     @autoreleasepool
  6     {
  7 #pragma mark - NSString
  8         char *s = "Hello Objective-C";//C语言的字符串格式
  9         NSString *str = @"Hello";
 10         //OC、C字符串的转换:C-->OC
 11         NSString *str1 = [NSString stringWithUTF8String:s];//UTF8是一种编码格式
 12         NSLog(@"str1 = %@", str1);
 13         //OC-->C
 14         NSLog(@"str2 = %s", [str UTF8String]);
 15         
 16         //创建字符串
 17         NSString *str3 = @"IOS";
 18         NSString *str4 = [[NSString alloc] init];
 19         str4 = @"IOS";
 20         // 格式化字符串
 21         int a = 10;
 22         int b = 20;
 23         NSString *str5 = [NSString stringWithFormat:@"a = %d b = %d", a, b];
 24         NSLog(@"str5 = %@", str5);
 25         //拼接字符串
 26         NSString *str6 = [[str5 stringByAppendingString:@" and "] stringByAppendingString:str3];
 27         NSLog(@"str6 = %@", str6);
 28         //大小写转换
 29         //转换小写
 30         NSString *str7 = @"aBcdEFG";
 31         NSString *str8 = [str7 lowercaseString];
 32         NSLog(@"str8 = %@", str8);
 33         //转换大写
 34         NSString *str9 = [str7 uppercaseString];
 35         NSLog(@"str9 = %@", str9);
 36         
 37         //前缀和后缀的判断
 38         NSString *str10 = @"www.songlei.com";
 39         //判断前缀
 40         BOOL hasPrefix = [str10 hasPrefix:@"www."];
 41         if (hasPrefix)
 42         {
 43             NSLog(@"有对应前缀");
 44         }
 45         else
 46         {
 47             NSLog(@"没有对应前缀");
 48         }
 49         //判断后缀
 50         BOOL hasSuffix = [str10 hasSuffix:@".com"];
 51         if (hasSuffix)
 52         {
 53             NSLog(@"有对应后缀");
 54         }
 55         else
 56         {
 57             NSLog(@"没有对应后缀");
 58         }
 59         
 60         //判断两个字符串是否相同
 61         NSString *str11 = @"Hello";
 62         NSString *str12 = @"Hello ";
 63         if ([str11 isEqualToString:str12])
 64         {
 65             NSLog(@"两个字符串一致");
 66         }
 67         else
 68         {
 69             NSLog(@"两个字符串不一致");
 70         }
 71         
 72         //***比较字符串***
 73 //        NSComparisonResult
 74         
 75         //分割//按照指定字符分割字符串
 76         NSString *str13 = @"a, b, c, d, e, f";
 77         NSArray *strArray = [str13 componentsSeparatedByString:@", "];
 78         for (NSString *str in strArray)
 79         {
 80             NSLog(@"str = %@", str);
 81         }
 82         //按照指定范围分割字符串
 83         NSRange range = NSMakeRange(3, 9);
 84         NSString *str14 = [str13 substringWithRange:range];
 85         NSLog(@"str14 = %@", str14);
 86         //从某一位开始截取后面的字符串
 87         NSString *str15 = [str13 substringFromIndex:9];
 88         NSLog(@"str15 = %@", str15);
 89         //从开头截取到某一位
 90         NSString *str16 = [str13 substringToIndex:9];
 91         NSLog(@"str16 = %@", str16);
 92         //将字符串拆分为每一个字符
 93         int j = (int)[str13 length];
 94         for(int i = 0; i < j; ++i)
 95         {
 96             NSLog(@"%c", [str13 characterAtIndex:i]);
 97         }
 98         
 99         //查找
100         NSString *str17 = @"ab cd ef gh ij ab";
101         //查找指定字符串的位置
102         NSRange range1 = [str17 rangeOfString:@"ab"];//(这个查找是一个正向查找)
103         NSLog(@"range1.location:%ld range1.length:%ld", range1.location, range1.length);
104         
105         //替换
106         NSString *str18 = @"Hello huashan, Hello songlei";
107         //替换某一个范围的内容
108         NSString *str19 = [str18 stringByReplacingCharactersInRange:NSMakeRange(0, 5) withString:@"你好"];
109         NSLog(@"str19 = %@", str19);
110         //用指定字符串替换字符串中的子串
111         /*
112          stringByReplacingOccurrencesOfString - 源字符串中要被替换的内容 withString - 替换字符串
113          */
114         NSString *str20 = [str18 stringByReplacingOccurrencesOfString:@"Hello" withString:@"Hi"];
115         NSLog(@"str20 = %@", str20);
116         
117 #pragma mark - NSURL(路径类)
118         // 读取文件
119         //文件来源:1.本地文件。2.网络文件
120         //路径类
121         NSString *str21 = @"www.baidu.com";
122         //网络路径
123         NSURL *httpURL = [NSURL URLWithString:str21];
124         //本地路径
125         NSURL *fileURL = [NSURL fileURLWithPath:str21];
126         //读取网络文件
127         NSString *httpStr = [NSString stringWithContentsOfURL:httpURL encoding:NSUTF8StringEncoding error:nil];
128         NSLog(@"httpURL = %@", httpStr);
129         //读取本地文件
130         NSString *fileStr = [NSString stringWithContentsOfFile:@"/Users/hskj/Desktop/Test.txt" encoding:NSUTF8StringEncoding error:nil];    //Test.txt自己创建的纯文本文件,放在了桌面上
131         NSLog(@"fileStr = %@", fileStr);    //fileStr = Hello IOS Songlei
132         
133         //写入文件
134         NSString *str22 = @"Hello TravelColor";
135         BOOL isOk = [str22 writeToFile:@"/Users/hskj/Desktop/Demo.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
136         if (isOk)
137         {
138             NSLog(@"文件写入成功");
139         }
140         else
141         {
142             NSLog(@"文件写入失败");
143         }
144         
145     }
146     return 0;
147 }
 1 2016-08-10 14:42:32.165 FoundationFrame[1117:177889] str1 = Hello Objective-C
 2 2016-08-10 14:42:32.166 FoundationFrame[1117:177889] str2 = Hello
 3 2016-08-10 14:42:32.166 FoundationFrame[1117:177889] str5 = a = 10 b = 20
 4 2016-08-10 14:42:32.166 FoundationFrame[1117:177889] str6 = a = 10 b = 20 and IOS
 5 2016-08-10 14:42:32.167 FoundationFrame[1117:177889] str8 = abcdefg
 6 2016-08-10 14:42:32.167 FoundationFrame[1117:177889] str9 = ABCDEFG
 7 2016-08-10 14:42:32.167 FoundationFrame[1117:177889] 有对应前缀
 8 2016-08-10 14:42:32.167 FoundationFrame[1117:177889] 有对应后缀
 9 2016-08-10 14:42:32.167 FoundationFrame[1117:177889] 两个字符串不一致
10 2016-08-10 14:42:32.167 FoundationFrame[1117:177889] str = a
11 2016-08-10 14:42:32.167 FoundationFrame[1117:177889] str = b
12 2016-08-10 14:42:32.167 FoundationFrame[1117:177889] str = c
13 2016-08-10 14:42:32.167 FoundationFrame[1117:177889] str = d
14 2016-08-10 14:42:32.167 FoundationFrame[1117:177889] str = e
15 2016-08-10 14:42:32.167 FoundationFrame[1117:177889] str = f
16 2016-08-10 14:42:32.168 FoundationFrame[1117:177889] str14 = b, c, d, 
17 2016-08-10 14:42:32.168 FoundationFrame[1117:177889] str15 = d, e, f
18 2016-08-10 14:42:32.168 FoundationFrame[1117:177889] str16 = a, b, c, 
19 2016-08-10 14:42:32.168 FoundationFrame[1117:177889] a
20 2016-08-10 14:42:32.168 FoundationFrame[1117:177889] ,
21 2016-08-10 14:42:32.168 FoundationFrame[1117:177889]  
22 2016-08-10 14:42:32.168 FoundationFrame[1117:177889] b
23 2016-08-10 14:42:32.168 FoundationFrame[1117:177889] ,
24 2016-08-10 14:42:32.168 FoundationFrame[1117:177889]  
25 2016-08-10 14:42:32.169 FoundationFrame[1117:177889] c
26 2016-08-10 14:42:32.169 FoundationFrame[1117:177889] ,
27 2016-08-10 14:42:32.169 FoundationFrame[1117:177889]  
28 2016-08-10 14:42:32.169 FoundationFrame[1117:177889] d
29 2016-08-10 14:42:32.169 FoundationFrame[1117:177889] ,
30 2016-08-10 14:42:32.169 FoundationFrame[1117:177889]  
31 2016-08-10 14:42:32.169 FoundationFrame[1117:177889] e
32 2016-08-10 14:42:32.169 FoundationFrame[1117:177889] ,
33 2016-08-10 14:42:32.169 FoundationFrame[1117:177889]  
34 2016-08-10 14:42:32.169 FoundationFrame[1117:177889] f
35 2016-08-10 14:42:32.169 FoundationFrame[1117:177889] range1.location:0 range1.length:2
36 2016-08-10 14:42:32.169 FoundationFrame[1117:177889] str19 = 你好 huashan, Hello songlei
37 2016-08-10 14:42:32.170 FoundationFrame[1117:177889] str20 = Hi huashan, Hi songlei
38 2016-08-10 14:42:32.186 FoundationFrame[1117:177889] httpURL = (null)
39 2016-08-10 14:42:32.186 FoundationFrame[1117:177889] fileStr = Hello IOS Songlei
40 2016-08-10 14:42:32.245 FoundationFrame[1117:177889] 文件写入成功
41 Program ended with exit code: 0