码迷,mamicode.com
首页 > 其他好文 > 详细

Objective-C字符串

时间:2017-09-15 09:59:00      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:select   利用   字符串转换   mst   encoding   字符   选中   app   文档   

(1)创建字符串

NSString* str = @"Hello World";

NSString* str = [ [NSString alloc] initWithUTF8String:"Hello World"]; //C语言字符串

NSString* str = [ [NSString alloc] initWithFormat: @"Hello %d", 10];

还可以利用initWithData:encoding:直接解码二进制数据

 

(2)字符串转换成其它数据类型

NSString* str = @"100";

int intVar = str.intValue;

其它数据类型相似的方法。

 

(3)字符串连接

NSString* str = @"Hello";

NSString* str1 = [ [str stringByAppendingString:@" World"];

还可以利用stringByAppendingFormat等等。

 

(4)字符串的截取

NSString* str = @"Hello World";

NSString* str1 = [str substringToIndex:5]; //取前五个字符

NSString* str1 = [str substringFromIndex:6]; //取后五个字符,注意index是6

NSRange r = {1,2}; //or NSRangeFromString(@"{1,2}")

NSString* str1 = [str substringWithRange:r];

 

注:在MAC上查看帮助文档的方式是三指点击类型,或者选中类型在help菜单中选择quick help for selected item

 

Objective-C字符串

标签:select   利用   字符串转换   mst   encoding   字符   选中   app   文档   

原文地址:http://www.cnblogs.com/jacky1982/p/7524161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!