iOS学习(OC语言)知识点整理

一、OC字符串的操作

1)OC中字符串分为两种:

1、不可变字符串NSString:不能修改对象内容,但是可以改变对象的指针。

2、可变字符串NSMutableString:可以修改对象内容。

二、NSString 不可变字符串的操作

1)将字符串常量对象直接赋值给字符串引用 NSString *str1=@"hello"; 字符串对象的输出格式:  NSLog(@"str1=%@",str1)。

2)initWithString可将OC中的字符串对象构建字符串引用   NSString *str2=[[NSString alloc]initWithString:str1];

3)initWithUTF8String可将C语言的字符串创建OC的字符串对象,将C字符串转换为OC字符串:

NSString *str3=[[NSString alloc]initWithUTF8String:"iOS"];

4)initWithFormat可将OC的格式化字符串创建OC的字符串对象 例如:

 int age=;
NSString *name=@"KingKong";
NSString *str4=[[NSString alloc]initWithFormat:@"name is %@,age is %d",name,age];

5)可使用.length方法获取字符串的长度   NSUInteger len= str1.length;

6)containsString 可用于判断字符串中是否含有某个字符 例如:

 NSString *str=@"hello china";
BOOL rest=[str containsString:@"china"];//结果YES

7)characterAtIndex可根据下标取出某个字符 如:

NSString *str1=@"hello"; unichar c= [str1 characterAtIndex:];
//结果为:h

8)compare用于比较两个字符串,该方法区分大小写, 返回结果为NSComparisonResult 枚举类型数据 枚举值有

1、NSOrderedAscending 表示前一个字符串小于后一个字符串

2、NSOrderedSame 表示两个字符串相等

3、NSOrderedDescending 表示前一个字符串大于后一个字符串

实例代码:

NSString *str1=@"hello";
NSString *str3=[[NSString alloc]initWithUTF8String:"iOS"];
NSComparisonResult cmp=[str1 compare:str3];
if(cmp==NSOrderedAscending){
NSLog(@"%@<%@",str1,str3);
}else if (cmp==NSOrderedSame){
NSLog(@"%@=%@",str1,str3);
}else if (cmp==NSOrderedDescending){
NSLog(@"%@>%@",str1,str3);
}//结果:hello<iOS

9)caseInsensitiveCompare 不区分大小写比较字符串;比较字符串,可以设置比较选项options:

1、NSNumericSearch:如果字符串中有数字, 按数字大小比较,例如:ios5<iso12

2、NSCaseInsensitiveSearch:不区分大小写比较,例如iOS7=ios7

实例代码:

 str1=@"iOS7";
str3=@"ios7";
NSComparisonResult cmp=[str1 caseInsensitiveCompare:str3];
cmp=[str1 compare:str3 options:NSCaseInsensitiveSearch];
if(cmp==NSOrderedAscending){
NSLog(@"%@<%@",str1,str3);
}else if (cmp==NSOrderedSame){
NSLog(@"%@=%@",str1,str3);
}else if (cmp==NSOrderedDescending){
NSLog(@"%@>%@",str1,str3);
} //结果:iOS7=ios7

10)isEqualToString 比较2个字符串内容是否相等,返回BOOL(YES/NO)值

实例代码:

 NSString *str1=@"hello";
NSString *str2=[[NSString alloc]initWithString:str1];
if([str1 isEqualToString:str2]){
NSLog(@"%@ equal %@",str1,str2);
}//结果:hello equal hello

11)uppercaseString 将小写字母字符串转为大写 例如:

 NSString *str1=@"hello";
NSString *str6=[str1 uppercaseString];// 结果为:HELLO

12)lowercaseString 将大写字母字符串转为小写 。

13)hasPrefix 判断是否以某个字符串开头(作为前缀)例如 :

NSString  str1=@"www.baidu.com";
if([str1 hasPrefix:@"www"]){
NSLog(@"yes");
}// 结果:yes

14)hasSuffix 判断是否以某个字符串结尾(后缀)例如:

  NSString  str1=@"www.baidu.com";
if([str1 hasSuffix:@"com"]){
NSLog(@"yes");
}//结果:yes

15)substringFromIndex 从某个下标开始取子字符串(到结束)例如:

 NSString str1=@"This is string A";
NSString *res=[str1 substringFromIndex:]; //结果:his is string A

16)substringToIndex 从第一个字符开始取到某个下标的子字符串,不包括当前数字对应的字符,取当前数字下标前面字符

例如:

 NSString  str1=@"This is string A";
NSString *res=[str1 substringToIndex:];//结果:Th
[[str1 substringFromIndex:] substringToIndex:];//结果: strin

17)substringWithRange 根据范围返回子字符串

NSRange range={,};//直接给范围值
range=NSMakeRange(, );//通过函数构造一个范围的变量
NSString str1=@"This is string A";
NSString res=[str1 substringWithRange:range]; //结果:strin

18)rangeOfString 在字符串中查找子字符串,返回这个字符串的所在位置以及长度 NSRange

实例代码:

 NSString  str1=@"This is string A";
NSRange range=[str1 rangeOfString:@"string"]; //结果:{8,6}
if(range.location!=NSNotFound){
NSLog(@"find");
}//结果:find

19)使用 #if 0  代码段… #endif 可以让系统跳过某段代码不执行,例如:

 #if 0
NSLog(@"hello world!");
#endif
NSLog(@"我是第二段代码!");
//执行结果:我是第二段代码!

20)intValue、longValue、floatValue、doubleValue 可将字符串类型的数字转换为数值类型的数字
 例如:

 NSString *str=@“”;
int a=[str intValue];

21)NSString 多个字符串的拼接

实例代码:

   NSString *str1=@"My name is";
NSString *str2=@"John Zhang";
NSString *str3=@"I am 45";
NSString *s1=[NSString stringWithFormat:@"%@ %@ %@",str1,str2,str3];
NSLog(@"s1=%@",s1); //结果:s1=My name is John Zhang I am 45

22)initWithContentsOfFile  将文件内容读取成字符串对象, 第一个参数是文件名(绝对路径),第二个参数是编码类型,
    第三个参数是错误回调信息  例如:

 NSString *path=@"/Documents/ocfile/Person.h" ;
NSString *content=[[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

23)直接打印对象,默认调用description方法,显示对象的类名和地址,可以重写description方法,
       注意:如果直接输出类对象中含有中文内容会有乱码,所以含中文内容的类对象必须遍历输出。

三、NSMutableString 可变字符串的操作

1)initWithString 用不可变字符串创建可变字符串对象 例如:

 NSString *str=@"This is string B";
NSMutableString *mStr=[[NSMutableString alloc]initWithString:str];

2)insertString ... atIndex..在指定下标位置插入一个字符串 例如:

 NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];
[mStr insertString:@"hello " atIndex:]; //结果:hello This is string B

3)appendString 在字符串后面追加字符串 例如:

  NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];
[mStr appendString:@"shanghai"];//结果:This is string B shanghai

4)appendFormat 在字符串后面追加一个格式化字符串 例如:

 NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];
[mStr appendFormat:@" %d",];//结果:This is string B 20

5)deleteCharactersInRange 将指定范围的字符串删除 例如:

  NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];
[mStr deleteCharactersInRange:NSMakeRange(, )];// 结果: s string B

6)replaceCharactersInRange 将指定范围的字符串用新的字符串替换 例如:

 NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];
[mStr replaceCharactersInRange:NSMakeRange(, ) withString:@"IS"]; //结果:ThIS is string B
//将字符串中所有的is替换为IS
NSRange range=[mStr rangeOfString:@"is"];
while (range.location!=NSNotFound) {
[mStr replaceCharactersInRange:range withString:@"IS"];
range=[mStr rangeOfString:@"is"];
}

7)replaceOccurrencesOfString 将字符串中指定范围内所有的is替换成IS 例如:

  NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];
[mStr replaceOccurrencesOfString:@“is” withString:@“IS” options: range:NSMakeRange(, mStr.length)];
//结果:ThIS IS string B

8)setString 修改字符串内容 例如:

  NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];
[mStr setString:@"hello"]; //结果:hello

9)NSMutableString 实现多个字符串的拼接  
实例代码:

 NSString *str1=@"My name is";
NSString *str2=@"John Zhang";
NSString *str3=@"I am 45";
NSMutableString *s2=[[NSMutableString alloc]initWithString:str1]; [s2 appendString:str2];
[s2 appendString:@" "];
[s2 appendString:str3];
NSLog(@"s2=%@",s2); //结果:s2=My name isJohn Zhang I am 45

iOS阶段学习第14天笔记(NSString与NSMutableString)的更多相关文章

  1. iOS 阶段学习第11天笔记(OC基础知识)

    iOS学习(OC语言)知识点整理 一.OC基础知识 1)#import  用于导入头文件,预处理阶段加载引用,只加载一次. 2)OC 依赖于Foundation框架下的头文件Foundation.h, ...

  2. iOS阶段学习第29天笔记(UITextField的介绍)

    iOS学习(UI)知识点整理 一.关于UITextField的介绍 1)概念: UITextField 是用于接收用户输入的一个控件 2)UITextField  初始化实例代码: //创建一个UIt ...

  3. iOS阶段学习第17天笔记(NSFileManager-NSFileHandle-文件操作)

    iOS学习(OC语言)知识点整理 一.单例模式 1)单例是一种编程思想,一个设计模式,与语言无关在采用了单例对象的应用程序中,需要单例类自行提供实例化单例对象, 不管实例化单例对象多少次,只有一个对象 ...

  4. iOS 阶段学习第七天笔记(函数、递归)

     iOS学习(C语言)知识点整理笔记 一.函数 1)概念:具有特定功能的代码块的封装 2)函数的定义: 函数类型+函数名(形参列表) 函数类型 函数名(形参类型1  形参名1,形参类型2   形参名2 ...

  5. iOS阶段学习第三天笔记(运算符)

    iOS学习(C语言)知识点整理笔记 1.运算符 一.算术运算符 1)表达式由变量.常量.运算符构成,有确定的类型和值 2)算术运算符包括: +(加),-(减),*(乘),/(除),%(模) 3)算术运 ...

  6. iOS 阶段学习第三天笔记(运算符)

    iOS学习(C语言)知识点整理笔记 1.运算符 一.算术运算符 1)表达式由变量.常量.运算符构成,有确定的类型和值 2)算术运算符包括: +(加),-(减),*(乘),/(除),%(模) 3)算术运 ...

  7. iOS阶段学习第34天笔记(UI小组件 UISegment-UISlider-UIStepper-UIProgressView-UITextView介绍)

    iOS学习(UI)知识点整理 一.UI小组件 1.UISegmentedControl 分段选择器  实例代码 - (void)viewDidLoad { [super viewDidLoad]; / ...

  8. iOS阶段学习第32天笔记(页面传值方法介绍)

    iOS学习(UI)知识点整理 一.界面传值方法 1.方法一  Block传值  通过SubView视图的Block向View视图传值改变View视图的背景色 实例代码: 1)SubViewContro ...

  9. iOS阶段学习第30天笔记( UIViewController—Delegate(代理) )

    iOS学习(UI)知识点整理 一.UIViewController的介绍 1)概念:UIViewController 即视图控制器,用来管理和控制页面跳转的一个类 ,iOS里面采用了MVC的体系结构, ...

随机推荐

  1. Unity3d使用经验总结 数据驱动篇

    我这里说的数据驱动,不是指某种框架,某种结构,或者某种编码方式. 我要说的,是一种开发方式. 大家都知道,U3D中,我们可以为某个对象编写一个脚本,然后将这个脚本挂在对象上,那这个对象就拥有了相应的能 ...

  2. 第十六回 IoC组件Unity续~批量动态为Unity添加类型和行为

    回到目录 之前的一篇Unity的文章主要是基本的实现,并没有什么特别的地方,使用Unity可以方便的实现应用程序的IoC控制反转,这给我们的应用程序在耦合度上变得高了,同时可测试性加强了,当然,这些的 ...

  3. 基础才是重中之重~Data层如何调用BLL层的方法,如果觉得奇怪请看本文章

    回到目录 看似不伦不类 这个题目有点不伦不类,或者说有点伪模式了,不错,确实是这样,我们正确的开发思维是WEB层->BLL层->DATA层,每个层有对它下层的引用,下层不能引用上层,因为这 ...

  4. EF架构~在T4模版中为所有属性加默认值

    回到目录 在项目开发过程中,出现了一个问题,就是新添加一个非空字段后,原来的程序逻辑需要被重新修改,即将原来的字段添加到程序里,这种作法是非常不提倡的,所以,我通过T4模版将原来的实体类小作修改,解决 ...

  5. Atitit 作用域的理解attilax总结

    Atitit 作用域的理解attilax总结 1.1. 作用域是指对某一变量和方法具有访问权限的代码空间, 1 1.2. 作用域的使用提高了程序逻辑的局部性,增强程序的可靠性,减少名字冲突.1 1.3 ...

  6. Atitit mtp ptp rndis midi协议的不同区别

    Atitit mtp ptp rndis midi协议的不同区别 1. PTP:1 2. MTP:1 3. Mtp 与usb区别2 4. 不过和UMS相比,MTP也有不足之处:3 5.  MTP协议介 ...

  7. 使用maven下载jar包的source和javadoc

    使用maven菜单下载sources和javadocs没什么反应,还是命令给力. 使用参数下载源码包与doc包: -DdownloadSources=true 下载源代码jar -DdownloadJ ...

  8. KlayGE 4.4中渲染的改进(四):SSSSS

    转载请注明出处为KlayGE游戏引擎,本文的永久链接为http://www.klayge.org/?p=2774 本系列的上一篇提到了KlayGE 4.4将会出现的高质量地形渲染.本篇仍讲一个新功能, ...

  9. Redis基础介绍及安装示例

    1.基本概念 Redis是由Salvatore Sanfilippo(意大利)开发的一个开源的高性能键值存储数据库,于2009年发布第一个版本并与同一年开源,官方站点:http://www.redis ...

  10. ZooKeeper官方文档翻译——ZooKeeper Overview 3.4.6

    ZooKeeper ZooKeeper: A Distributed Coordination Service for Distributed Applications (针对分布式应用的分布式调度服 ...