- //
- // main.m
- // NSString-1
- //
- // Created by yusian on 14-3-22.
- // Copyright (c) 2014年 小龙虾论坛. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- int main()
- {
- /**********NSString的几种常用方法*************/
- // 1、最常用的创建字符串方法
- NSString * str = @"yusian.com";
-
- NSLog(@"str = %@", str);
-
- // 2、不常用的创建字符串方法
- NSString * str1 = [NSString stringWithFormat:@"my age is %d", 10];
-
- NSLog(@"str1 = %@",str1);
-
- NSString * str2 = [[NSString alloc] initWithFormat:@"age is %d", 10];
-
- NSLog(@"str2 = %@", str2);
-
- // 3、C语言字符串创建OC字符串对象
- NSString * str3 = [NSString stringWithUTF8String:"c to oc"];
-
- NSLog(@"str3 = %@", str3);
-
- // 4、OC字符串创建C语言字符串
- const char * cs = [[NSString stringWithFormat:@"oc to c"] UTF8String];
-
- NSLog(@"cs = %s", cs);
-
- // 5、以字符串的方式读取文件内容(注意两个方法的区别)
- // 方法一:[NSString stringWithContentsOfFile:<#(NSString *)#> usedEncoding:<#(NSStringEncoding *)#> error:<#(NSError *__autoreleasing *)#>];
- // 方法二:[NSString stringWithContentsOfFile:<#(NSString *)#> encoding:<#(NSStringEncoding)#> error:<#(NSError *__autoreleasing *)#>];
-
- NSString * str4 = [NSString stringWithContentsOfFile:@"/Users/yusian/Desktop/1.txt" encoding:NSUTF8StringEncoding error:nil];
- // NSUTF8StringEncoding可兼容中文编码
-
- NSLog(@"str4 = %@", str4);
-
- // 6、以字符串的方式读取URL
- // > 统一资源定位符(Uniform Resource Locator,缩写为URL)
- // > 先定义一个URL,URL可以写"http://abc.com"、"file:///User/yusian/Desktop/..."、"ftp://abc.com"等等;
- NSURL *url = [NSURL URLWithString:@"http://baidu.com"];
-
- // 将URL传到NSString的方法当中
- NSString * str5 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
-
- NSLog(@"str5 = %@", str5);
-
-
-
- /**********NSMutableString的几种常用方法***********/
-
- // 1、创建可变字符串的一般方法,可变字符串继承于字符串
- NSMutableString * mStr = [NSMutableString stringWithFormat:@"my age is %d", 10];
-
- NSLog(@"mStr = %@", mStr);
-
- // 2、可变字符串可附加内容,这是字符串所不具备的
- [mStr appendFormat:@" and height is %dcm.", 173];
-
- NSLog(@"mStr = %@", mStr);
-
- // 3、删除字符串中的特定字符
- // 字符串获取子串范围的方法
- NSRange range = [mStr rangeOfString:@"my "];
- // 通过范围删除字符串中的子串,也可直接写范围,固定范围容易因为字符串的变动而产生误差
- [mStr deleteCharactersInRange:range];
-
- NSLog(@"mStr = %@", mStr);
-
- /***********字符串的输出************/
-
- NSString * string = @"yusian.com";
-
- // NSSring及子类都具备该方法
- //[string writeToFile:<#(NSString *)#> atomically:<#(BOOL)#> encoding:<#(NSStringEncoding)#> error:<#(NSError *__autoreleasing *)#>]
- [string writeToFile:@"/Users/yusian/Desktop/yusian.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
-
-
- return 0;
- }
复制代码 运行结果:
2014-03-22 16:48:23.076 NSString-1[3340:303] str = yusian.com 2014-03-22 16:48:23.077 NSString-1[3340:303] str1 = my age is 10 2014-03-22 16:48:23.078 NSString-1[3340:303] str2 = age is 10 2014-03-22 16:48:23.078 NSString-1[3340:303] str3 = c to oc 2014-03-22 16:48:23.078 NSString-1[3340:303] cs = oc to c 2014-03-22 16:48:23.079 NSString-1[3340:303] str4 = yusian.com www.yusian.com 2014-03-22 16:48:23.290 NSString-1[3340:303] str5 = <html> <meta http-equiv="refresh" content="0;url=http://www.baidu.com/"> </html> 2014-03-22 16:48:23.290 NSString-1[3340:303] mStr = my age is 10 2014-03-22 16:48:23.290 NSString-1[3340:303] mStr = my age is 10 and height is 173cm. 2014-03-22 16:48:23.290 NSString-1[3340:303] mStr = age is 10 and height is 173cm. Program ended with exit code: 0
|