iOS阶段学习第14天笔记(NSString与NSMutableString)

摘要:
iOS学习知识点整理一、OC字符串的操作1)OC中字符串分为两种:1、不可变字符串NSString:不能修改对象内容,但是可以改变对象的指针。

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

一、OC字符串的操作

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

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

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

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

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

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

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

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

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

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

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

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

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

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

NSString *str1=@"hello"; unichar c= [str1 characterAtIndex:0];
//结果为: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

实例代码:

1 str1=@"iOS7";
2 str3=@"ios7";
3 NSComparisonResult cmp=[str1 caseInsensitiveCompare:str3];
4 cmp=[str1 compare:str3 options:NSCaseInsensitiveSearch];
5 if(cmp==NSOrderedAscending){
6 NSLog(@"%@<%@",str1,str3);
7 }else if (cmp==NSOrderedSame){
8 NSLog(@"%@=%@",str1,str3);
9 }else if (cmp==NSOrderedDescending){
10 NSLog(@"%@>%@",str1,str3);
11 } //结果:iOS7=ios7

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

实例代码:

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

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

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

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

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

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

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

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

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

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

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

例如:

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

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

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

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

实例代码:

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

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

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

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

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

21)NSString 多个字符串的拼接

实例代码:

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

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

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

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

三、NSMutableString 可变字符串的操作

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1 NSString *str1=@"My name is";
2 NSString *str2=@"John Zhang";
3 NSString *str3=@"I am 45";
4 NSMutableString *s2=[[NSMutableString alloc]initWithString:str1]; 
5 
6 [s2 appendString:str2];
7 [s2 appendString:@" "];
8 [s2 appendString:str3];
9 NSLog(@"s2=%@",s2);   //结果:s2=My name isJohn Zhang I am 45

免责声明:文章转载自《iOS阶段学习第14天笔记(NSString与NSMutableString)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Docker Volume 之权限管理(一)manjaro pacman 常用命令下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Java消息系统简单设计与实现

前言:由于导师在我的毕设项目里加了消息系统(本来想水水就过的..),没办法...来稍微研究研究吧..简单简单... 需求分析 我的毕设是一个博客系统,类似于简书这样的,所以消息系统也类似,在用户的消息里包含了有:喜欢和赞、评论、关注、私信这样的一类东西,这样的一个系统应该包含以下的功能: 当用户评论/关注/点赞时能够通知到被评论/关注/点赞的用户,并...

AccountManager使用教程

API解读 这个类给用户提供了集中注冊账号的接口。用户仅仅要输入一次账户password后,就能够訪问internet资源。 不同的在线服务用不同的方式管理用户,所以account manager 为不同类型的账户提供了统一验证管理的方法,处理有效的账户的具体信息而且实现排序。比方Google,Facebook,Microsoft Exchange 各自...

Perl模式匹配

       Perl 内置的模式匹配让你能够简便高效地搜索大量的数据。不管你是在一个巨型的商业门户站点上用于扫描每日感兴趣的珍闻报道,还是在一个政府组织里用于精确地描述人口统计(或者人类基因组图),或是在一个教育组织里用于在你的 web 站点上生成一些动态信息,Perl 都是你可选的工具。这里的一部分原因是 Perl 的数据库联接能力,但是更重要的原因是...

Java中如何获取spring中配置文件.properties中属性值

通过spring配置properties文件 1 2 3 4 5 6 7 8 9 <bean id="propertyConfigurer" class="com.hapishop.util.ProjectDBinfoConfigurer"> <property name="ignoreResourceNotFound"value...

JAVA多线程提高八:线程锁技术

前面我们讲到了synchronized;那么这节就来将lock的功效。 一、locks相关类 锁相关的类都在包java.util.concurrent.locks下,有以下类和接口: |---AbstractOwnableSynchronizer |---AbstractQueuedLongSynchronizer |---AbstractQueued...

Java路径问题解决方案收集

Java路径中的空格问题 1, TestURL().class.getResource("").getPath()或TestURL().class.getResource("").getFile()获得的路径,不能被FileReader()和FileWriter()直接应用。   原因是URL对空格,特殊字符(%,#,[]等)和中文进行了编码处理。   例...