Objective-C( Foundation框架 一 字符串)
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重新给这个字符串赋值。而NSMutableString 创建赋值以后可以动态在该字符串上更改内容与长度。
// 创建字符串
NSString *str = @"jack";
NSString *s1 = [[NSString alloc] initWithFormat:@"age = %d" ,]; // c字符串转oc字符串
NSString *s2 = [[NSString alloc] initWithUTF8String:"jack"]; // oc 转 c
const char *c1 = [s2 UTF8String];
NSLog(@"%s", c1);
字符串的遍历
NSString *str = @"ni hao peng you";
for (int i = ; i < str.length; i++) {
unichar c = [str characterAtIndex:i];
NSLog(@"%c",c);
}
NSURL
// NSUTF8StringEncoding 用到中文可以用这个
// 传进来文件路径可以查看文件
NSString *s3 = [[NSString alloc] initWithContentsOfFile:"/Users/apple/Desktop/1.txt" encoding:NSUTF8StringEncoding error:nil];
/*URL :资源路径
协议头://路径
file://
ftp://
http://www.baidu.com
*/
// 在 iOS 程序访问 HTTP 资源时需要对 URL 进行 Encode
// NSURL *url = [[NSURL alloc] initWithString:@"file:///Users/apple/Desktop/1.txt"]; NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/1.txt"]; NSString *s4 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"s4=\n%@", s4);
NSString :不可变字符串
NSMutableString :可变字符串
字符串中拼接
// 创建字符串
NSMutableString *s = [NSMutableString stringWithFormat:@"my age is 23"];
// 拼接内容到s的后面
[s appendString:@" 3 21"];
字符串中删除元素
//创建字符串
NSMutableString *str = [NSMutableString stringWithString:@"haha 你好!!"];
//删除字符串中含"haha"的字符
[str deleteCharactersInRange: [str rangeOfString: @"haha"]];
NSLog(@"str = %@",str);
字符串查找
查找一个字符串在另一个字符串首次出现的位置
NSString *s = @"abciOSdefg";
NSString *s1 = @"iOS";
NSRange * range = [s rangeOfString:s1];
// 判断是否找到字符串
if(range.location!NSNotFound){
NSLog(@"%lu,%lu",range,location, range.length);
}else{
NSLog(@"没有找到字符串位置");
}
判断字符串是否相同
// 字符串是否相同
NSString *s1 = @"abc"; NSString *s2 = [NSString stringWithFormat:@"abc"]; if ([s1 isEqualToString:s2]) {
NSLog(@"相同");
}else{
NSLog(@"不相同");
}
字符串截取
NSString *str = @"";
// 从元素下标为3的位置开始往后截取,(包含下标为3的位置的元素)
NSString *str1 = [str substringFromIndex:];
NSLog(@"str1 = %@",str1);
// 从开始位置截取到3位(不包含下标为3的位置的元素)
NSString *str2 = [str substringToIndex:];
NSLog(@"str2 = %@",str2);
// 截取某段位置的字符进行输出
NSRange range ={,};
NSString *str3 = [str substringWithRange:range];
NSLog(@"str3 = %@",str3);
// 截取某段位置的字符
// 012345 789
NSString *str = @"aaaaa<你好>aaaaaa";
NSUInteger loc = [str rangeOfString:@"<"].location + ; // 传的位置
NSUInteger len = [str rangeOfString:@">"].location - loc; // 要截取的字符长度 NSRange r1 = {loc, len}; NSString *subStr = [str substringWithRange:r1];
NSLog(@"subStr = %@", subStr);
字符串的替换
// 字符串的替换
NSString *str = @"adadwffdtoiajdfa"; // str stringByReplacingOccurrencesOfString:@"源字符串" withString:@"要替换成的新内容"
// 把a替换成/
NSString * newStr = [str stringByReplacingOccurrencesOfString:@"a" withString:@"/"];
NSLog(@"newStr = %@", str); // 把字符串中的空格去掉
str = @" adad wff dt oiaj df a";
newStr = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"newStr = %@", newStr); // 把字符串中空格去掉,把星号替换成/
str = @"http ** www* baidu* com*"; newStr = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *newStr1 = [newStr stringByReplacingOccurrencesOfString:@"*" withString:@"/"];
NSLog(@"newStr1 = %@",newStr1);
Objective-C( Foundation框架 一 字符串)的更多相关文章
- Objective - c Foundation 框架详解2
Objective - c Foundation 框架详解2 Collection Agency Cocoa provides a number of collection classes such ...
- Objective-C语言Foundation框架
Mac OS X开发会使用Cocoa框架,它是一种支持应用程序提供丰富用户体验的框架,它实际上由:Foundation和Application Kit(AppKit)框架组成.iOS开发,会使用Coc ...
- OC 知识:Foundation 框架及相关类详尽总结
本文用来介绍Foundation框架的相关知识,以及Foundation框架所提供类的相关知识总结. 1. 框架介绍 框架是由很多类.方法.函数和文档按照一定的逻辑组织起来的集合,以使开发程序变得更加 ...
- Foundation框架—字符串
Foundation框架—字符串 一.Foundation框架中一些常用的类 字符串型: NSString:不可变字符串 NSMutableString:可变字符串 集合型: 1) NSArray:O ...
- 李洪强iOS之Foundation框架—字符串
Foundation框架—字符串 一.Foundation框架中一些常用的类 字符串型: NSString:不可变字符串 NSMutableString:可变字符串 集合型: 1) NSArray:O ...
- OC Foundation框架—字符串
一.Foundation框架中一些常用的类 字符串型: NSString:不可变字符串 NSMutableString:可变字符串 集合型: 1) NSArray:OC不可变数组 NSMutableA ...
- Objective-C( Foundation框架 一 常见的结构体)
常见的结构体 (NSPoint,CGPoint).(NSRange,CGRange).(NSSize,CGSize) 苹果官方推荐使用CG开头的结构体 NSRange是Foundation框架中常见的 ...
- iOS开发系列—Objective-C之Foundation框架
概述 我们前面的章节中就一直新建Cocoa Class,那么Cocoa到底是什么,它和我们前面以及后面要讲的内容到底有什么关系呢?Objective-C开发中经常用到NSObject,那么这个对象到底 ...
- Foundation框架-NSString和NSMutableString
可变与不可变的字符串 --1-- Foundation框架介绍 1.1 框架介绍 --2-- NSString 2.1 NSString介绍及使用 2.2 NSString创建方式 2.3 从文件中 ...
随机推荐
- pepperflash
sudo apt-get install pepperflashplugin-nonfree sudo update-pepperflashplugin-nonfree --install
- 同事的游戏项目--Robocode-学习链接
Robocode机器人库学习链接:http://www.pudn.com/search_db.asp?keyword=Robocode 官网 :http://robocode.sourceforge. ...
- SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...
- Unity的安装和破解
网址:unity3d.com/cn/ unity的破解软件可以去unity圣典的网站上下载: 点击资源库,在资源库中找 下载过程中有时会提示需要对应的VS版本,忽略掉这个错误,并不需要最新的VS, ...
- 反编译ILSpy 无法显式调用运算符或访问器 错误处理方法 转
反汇编一个dll类库,导出的项目会报出很多bug,其中主要的就是“无法显式调用运算符或访问器”这个错误,看了一下,发现问题是在调用属性的时候,都 变成了方法,例如:pivotPoint.set_X(0 ...
- The import java.util cannot be resolved The import javax.servlet cannot be resolved
The import java.util cannot be resolved 原因:这是由于你的项目buildpath不对 解决方案:右键项目-------buildpath--------最下面那 ...
- WLST 命令和变量
下列部分将详细描述 WLST 命令和变量.主题包括: WSLT 命令类别概述 浏览命令 控制命令 部署命令 诊断命令 编辑命令 信息命令 生命周期命令 节点管理器命令 树命令 W ...
- UBIFS 文件系统分析1 - 磁盘结构【转】
转自:http://blog.csdn.net/kickxxx/article/details/7109662 版权声明:本文为博主原创文章,未经博主允许不得转载. ubifs磁盘结构 UBIFS文件 ...
- 结合Apache和Tomcat实现集群和负载均衡 JK 方式
本文基本参考自 轻松实现Apache,Tomcat集群和负载均衡,经由实操经历记录而成,碰到些出入,以及个别地方依据个人的习惯,所以在一定程度上未能保持原文的完整性,还望原著者海涵. 因原文中有较多的 ...
- 【20160924】GOCVHelper 图像处理部分(3)
//根据轮廓的圆的特性进行选择 vector<VP> selectShapeCircularity(Mat src,Mat& draw,vector<VP> c ...