您的位置:首页 > 移动开发 > IOS开发

自学iOS开发系列----OC(字符串)

2016-11-21 13:44 288 查看
OC的程序,依托于基础类库Foundation,这个框架里提供很多官方类。其中有三个基础的数据结构:字符串、数组、字典。OC兼容C的字符串,但是OC有自己的字符串,是一个类。声明的对象,既能存储一串字符,又能通过一些方法,管理存储的字符。

一、不可变字符串

1.创建字符串

C中用”” 表示字符串,OC中用@”” 表示字符串;

打印C字符串用%s,打印OC字符串用%@。

①字面量方法(常量赋值)

NSString * str1 = @"春风十里不如你";
NSLog(@"字面量赋值:%@",str1);


②实例化方法(对象方法)

NSString * str2 = [[NSString alloc] initWithString:@"再别康桥"];
NSLog(@"实例方法:%@",str2);


③用C字符串创建OC字符串

NSString * str3 = [[NSString alloc] initWithCString:"C String" encoding:NSUTF8StringEncoding];
NSLog(@"C字符串创建OC字符串:%@",str3);


④格式化字符串

NSString * str4 = [[NSString alloc] initWithFormat:@"%@-%@",@"升值",@"加薪"];
NSLog(@"格式化字符串:%@",str4);


⑤类方法

NSString * str5 = [NSString stringWithFormat:@"%d,%f,%@",521,13.14,@"我带你一生一世"];


2.求字符串长度

NSString * strLength = @"谈一场不分手的恋爱";
NSUInteger len = [strLength length];
NSLog(@"length = %lu",len);


3.通过索引获取相应的字符

NSString * strIndexCharacter = @"I love you forever";
unichar c = [strIndexCharacter characterAtIndex:7];
NSLog(@"%C",c);


4.判断两个字符串是否相等(BOOL 布尔值,非真即假)

NSString * strEqual1 = @"To live in hearts we leave is not to die";
NSString * strEqual2 = @"To live in hearts we leave is not to die";
BOOL isEqual= [strEqual1 isEqualToString:strEqual2];

if (god == TRUE) {
NSLog(@"两个字符串相等");
} else {
NSLog(@"两个字符串不相等");
}


5.字符串查找

NSString * strSeek = @"I love you";
NSRange range = [strSeek rangeOfString:@"love me"];
NSLog(@"love loc = %lu,len = %lu",range.location,range.length);

if (range.length == 0) {
NSLog(@"不存在这样的子串");
}


6.截取字符串

①按照范围截取

NSSstring * strSub = @"I love you forever !";
NSRange range = {2,4};
NSString * strRange = [strSub substringWithRange:range];
NSLog(@"按照范围截取字符串%@",strRange);


②从某位置截取到最后(包含起始位置)

NSString * str1 = [strSub substringFromIndex:3];
NSLog(@"%@",str1);


③从第一个字符截取到某位置(不包含结束位置)

NSString * str2 = [strSub substringToIndex:7];
NSLog(@"%@",str2);


7.判断前后缀

NSString * strFix = @"http://www.baidu.com/beatiful/girl.png";

BOOL tag1 = [str14 hasPrefix:@"http://"];
BOOL tag2 = [str14 hasSuffix:@".jpg"];

if (tag1) {
NSLog(@"是http://前缀,说明他是一个网址");
} else {
NSLog(@"不是http://前缀,说明他不是一个网址");
}

if (tag2) {
NSLog(@"是.jpg后缀,说明他是一张jpg图片");
} else {
NSLog(@"不是.jpg后缀,说明他不是一张jpg图片");
}


8.数字串转化为数字

NSString * strNum = @"5211314";
int ret = [strNum intValue];
NSLog(@"strNum:%d",ret);


9.大小写转换

①把所有小写字母转成大写

NSString * str = @"If you do not leave me.I will by yourside until the life end!";
NSString * upStr = [str uppercaseString];
NSLog(@"所有字母大写 = %@",upStr);


②把所有大写字母转成小写

NSString * str = @"If you do not leave me.I will by yourside until the life end!";
NSString * lowStr = [str lowercaseString];
NSLog(@"所有字母小写 = %@",lowStr);


③把每个单词的首字母变成大写(连续的字符会做为一个整体)

NSString * str = @"If you do not leave me.I will by yourside until the life end!";
NSString * capStr = [str capitalizedString];
NSLog(@"每个单词首字母大写 = %@",capStr);


10.从本地读取文件内容

/**
*  @param  path    文件的路径
*  @param  enc    文字编码
*  @param  error  读取文件失败 ,会返回一个错误对象,
*/

NSError * error = nil;
NSString * path = @"/Users/qianfeng/Desktop/未命名.rtf";
NSString * content = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

if (error) {
NSLog(@"error = %@",error);
} else {
NSLog(@"%@",content);
}


二、可变字符串

1.在指定位置插入子串

NSMutableString * mutableStr = [[NSMutableString alloc] initWithString:@"Hello I love you"];
[mutableStr insertString:@"World" atIndex:6];
NSLog(@"%@", mutableStr);


2.删除指定范围的子串

NSMutableString * mutableStr = [[NSMutableString alloc] initWithString:@"Hello I love you"];
NSRange range = {6,8};
[mutableStr deleteCharactersInRange:range];
NSLog(@"%@", mutableStr);


3.追加子串

NSMutableString * mutableStr = [[NSMutableString alloc] initWithString:@"Hello I love you"];
[mutableStr appendFormat:@" %d %.2f %@",1800,3.15,@"我爱你"];
NSLog(@"%@", mutableStr);


4.修改字符串

NSMutableString * mutableStr = [[NSMutableString alloc] initWithString:@"Hello I love you"];
[mutableStr setString:@"God bye"];
NSLog(@"%@", mutableStr);


5.替换字符串

NSMutableString * mutableStr = [[NSMutableString alloc] initWithString:@"Hello I love you"];
NSRange range = {6,7};
[mutableStr replaceCharactersInRange:range withString:@"world"];
NSLog(@"%@", mutableStr);


小练习

1.从字符串@”Welcome to Beijing!”, 中提取第3到第10个字符,生成字符串。

2.将可变字符串@”When I was young, I loved a girl in neighbor class.”中,从young提取到girl。替换成@”a teacher, I rather to teach student”。(要求不能手动计数)

参考答案(一万个读者有一万个哈姆雷特,一万个程序员有一万种编码风格,答案仅供参考)

1.从字符串@”Welcome to Beijing!”, 中提取第3到第10个字符,生成字符串。

NSString * str = @"Welcome to Beijing!";
NSRange range = {2,8};
NSString * strNew = [str substringWithRange:range];
NSLog(@"%@",str5);


2.将可变字符串@”When I was young, I loved a girl in neighbor class.”中,从young提取到girl。替换成@”a teacher, I rather to teach student “。(要求不能手动计数)

NSMutableString * mutableStr = [[NSMutableString alloc] initWithString:@"When I was young, I loved a girl in neighbor class."];
NSString * str = @"a teacher, I rather to teach student ";
NSString * startStr = @"young";
NSString * endStr = @"girl";

//求出起始位置
NSRange startRange = [mutableStr rangeOfString:startStr];

//求出截至位置
NSRange endRange = [mutableStr rangeOfString:endStr];

//求出需要替换的字符串位置
NSRange range = {startRange.location,endRange.location - startRange.location + endRange.length};
[mutableStr replaceCharactersInRange:range withString:str];
NSLog(@"%@",mutableStr);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios开发 字符串