1.创建NSString对象
-
void stringCreate() {
-
//这种方式创建,不需要释放内存
-
NSString *str1 = @"A String";//
-
//
-
NSString *str2 = [[NSString alloc] init];
-
str2 = @"B String";
-
[str2 release];
-
-
NSString *str3 = [[NSString alloc]initWithString:@"C string!"];
-
[str3 release];
-
-
//静态方法创建对象,不需要管理内存
-
str3 = [NSString stringWithString:@"c string!"];
-
-
NSString *str4 = [[NSString alloc] initWithUTF8String:"D string!"];
-
[str4 release];
-
-
NSString *str5 = [[NSString alloc] initWithFormat:
-
@"my age is %i and height is %.2f",28,1.65f];
-
NSLog(@"str5:%@",str5);
-
[str5 release];
-
}
2.NSString的导入与导出:从文件或url读取内容,以及写文件
-
void stringCreate2(){
-
NSString *path=@"/Users/luteresa/Desktop/hello.txt";
-
//NSString *str1 = [NSString stringWithContentsOfFile:path];
-
NSError *perror;
-
NSString *str2 = [NSString stringWithContentsOfFile:path encoding:(NSUTF8StringEncoding) error:&perror];
-
if (perror) {
-
NSLog(@"读取错误:%@",perror);
-
} else {
-
NSLog(@"%@",str2);
-
}
-
}
-
void stringCreate3(){
-
NSString *path = @"/Users/luteresa/Desktop/abc/abc.txt";
-
-
//NSURL *url = [NSURL URLWithString:@"file:///Users/luteresa/Desktop/abc.txt"];
-
// NSURL *url = [NSURL URLWithString:@""];
-
// NSString *str2 = [NSString stringWithContentsOfURL: url encoding:NSUTF8StringEncoding error:nil];
-
// //NSLog(@"%@",str2);
-
NSString *str = @"123456";
-
NSError *perror;
-
//[perror localizedDescription];
-
[str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&perror];
-
if (perror) {
-
NSLog(@"写入失败,%@", [perror localizedDescription]);
-
} else {
-
NSLog(@"写入成功");
-
}
-
}
-
void stringToFile(){
-
NSString *path = @"/Users/luteresa/Desktop/test.txt";
-
NSString *str1 = @"123457890";
-
[str1 writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
-
}
3.NSString常用方法:
(1) 大小写处理
点击(此处)折叠或打开
(NSString *)uppercaseString; //全部转为大写字符
-
-
(NSString *)lowercaseString; //全部转为小写字母
-
(NSString *)capitalizedString;//首字符变大写,其他变小写
-
(2)字符串比较
-
void compare() {
-
// 检测字符串的内容是否相同
-
BOOL result = [@"abc" isEqualToString:@"abc"];
-
NSLog(@"%i", result);
-
-
// NSOrderedAscending 右边的字符串比左边大
-
// NSOrderedSame 两个字符串的内容相同
-
// NSOrderedDescending 左边的字符串比右边的大
-
NSComparisonResult result2 = [@"abc" compare:@"Abc"];
-
if (result2 == NSOrderedSame) {
-
NSLog(@"两个字符串的内容相同");
-
} else if (result2 == NSOrderedAscending) {
-
NSLog(@"右边 > 左边");
-
} else if (result2 == NSOrderedDescending) {
-
NSLog(@"右边 < 左边");
-
}
-
}
还有一个忽略大小写的比较方法,用法余compare一样
-
- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
(3) 搜索字符串
-
void search() {
-
NSString *str = @"123456456.txt";
-
-
NSLog(@"是否以22开头:%i", [str hasPrefix:@"22"]);
-
NSLog(@"是否以txt结尾:%i", [str hasSuffix:@"txt"]);
-
-
// 搜索字符串
-
NSRange range = [str rangeOfString:@"456"];
-
// range.length == 0
-
if (range.location == NSNotFound) {
-
NSLog(@"不能找到");
-
} else {
-
NSLog(@"找到的范围是:%@", NSStringFromRange(range));
-
}
-
-
// 从尾部开始搜索字符串
-
range = [str rangeOfString:@"456" options:NSBackwardsSearch];
-
NSLog(@"%@", NSStringFromRange(range));
-
-
// 指定范围进行搜索
-
range = [str rangeOfString:@"456" options:NSBackwardsSearch range:NSMakeRange(1, 10)];
-
NSLog(@"%@", NSStringFromRange(range));}
(4) 字符串截取
-
void subString() {
-
NSString *str = @"123456";
-
-
// 从索引3开始截取到尾部(包括3)
-
NSLog(@"%@", [str substringFromIndex:3]);
-
-
// 从头部开始截取到索引3之前(不包括3)
-
NSLog(@"%@", [str substringToIndex:3]);
-
-
// 指定范围进行截取
-
NSRange range = NSMakeRange(2, 3);
-
NSLog(@"%@", [str substringWithRange:range]);
-
-
NSString *str2 = @"a-b-c-d-5";
-
NSArray *array = [str2 componentsSeparatedByString:@"-"];
-
NSLog(@"%@", array);
-
-
NSString *str3 = [array objectAtIndex:0];
-
NSLog(@"%@", str3);
-
}
(5)#pragma mark 与路径相关
-
void pathTest() {
-
// 快速创建一个自动释放的数组
-
NSMutableArray *components = [NSMutableArray array];
-
[components addObject:@"Users"];
-
[components addObject:@"MJ"];
-
[components addObject:@"Desktop"];
-
// 将数组中的所有字符串拼接成一个路径
-
NSString *path = [NSString pathWithComponents:components];
-
NSLog(@"%@", path);
-
-
// 将路径分解成一个数组
-
NSArray *cmps = [path pathComponents];
-
NSLog(@"%@", cmps);
-
-
// path是一个字符串常量,是不可变的
-
path = @"/users/mj/test";
-
// 判断是够为绝对路径(依据是前面有无/)
-
NSLog(@"%i", [path isAbsolutePath]);
-
NSLog(@"最后一个目录:%@", [path lastPathComponent]);
-
// 删除最后一个目录
-
NSLog(@"%@", [path stringByDeletingLastPathComponent]);
-
// 在最后面拼接一个目录
-
NSLog(@"%@", [path stringByAppendingPathComponent:@"abc"]);
-
}
(6)#pragma mark 拓展名处理
-
void extension() {
-
NSString *str = @"/User/MJ/test.txt";
-
-
NSLog(@"拓展名:%@", [str pathExtension]);
-
// 删除拓展名
-
NSLog(@"%@", [str stringByDeletingPathExtension]);
-
// 添加拓展名
-
NSLog(@"%@", [@"abc" stringByAppendingPathExtension:@"mp3"]);
-
-
//修改文件名
-
NSString *tmp,*str2 = @"/User/leon/hello.txt";
-
NSString *extensionName =[str2 pathExtension];
-
tmp = [[[[str2 stringByDeletingPathExtension]stringByDeletingLastPathComponent]
-
stringByAppendingPathComponent:@"world"]stringByAppendingPathExtension:extensionName];
-
-
NSLog(@"%@",tmp);
-
}
(7)#pragma mark 其他用法,NSString类与基本类型转换等
-
void other() {
-
NSString *str = @"12";
-
int a = [str intValue];
-
NSLog(@"%i", a);
-
-
// 计算字数,不是计算字符数
-
NSLog(@"length=%zi", [@"我是字符串123" length]);
-
-
// 取出对应的字符
-
unichar c = [@"abc" characterAtIndex:0];
-
NSLog(@"%c", c);
-
-
// 返回C语言中的字符串
-
const char *s = [@"abc" UTF8String];
-
NSLog(@"%s", s);
-
}
NSMutableString:NSString子类,可变字符串
-
#pragma mark 可变字符串的创建
-
void stringCreate() {
-
// 预先分配10个字数的存储空间
-
NSMutableString *str = [[NSMutableString alloc] initWithCapacity:10];
-
// 设置字符串内容
-
[str setString:@"1234"];
-
-
// 拼接一个字符串
-
[str appendString:@"567"];
-
// 拼接字符串
-
[str appendFormat:@"age is %i and height is %.2f", 27, 1.55f];
-
-
// 替换字符串
-
NSRange range = [str rangeOfString:@"height"];
-
//NSRange range = NSMakeRange(7, 3);
-
[str replaceCharactersInRange:range withString:@"no"];
-
-
// 插入字符串
-
[str insertString:@"abc" atIndex:2];
-
-
// 删除字符串
-
range = [str rangeOfString:@"age"];
-
[str deleteCharactersInRange:range];
-
-
NSLog(@"%@", str);
-
-
// 释放对象
-
[str release];
-
}
阅读(2284) | 评论(0) | 转发(0) |