objective-c 编程基础(3.4 Foundation框架之字符串)
来源:互联网 发布:算法设计最值 编辑:程序博客网 时间:2024/05/16 07:29
oc的NSString常用方法,如下图所示:
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... //字符串的初始化 NSString *str = @"你好";//字面量初始化 NSLog(@"str=%@",str); //使用format拼接字符串的格式 str = [NSString stringWithFormat:@"%@你好,我%i岁",@"小明",18]; NSLog(@"str=%@",str); //字符串的长度 int length = str.length; NSLog(@"str的长度为%i",length); //字符串转换基本数据类型 NSString *intNs = @"15"; int intVa = [intNs intValue]; NSLog(@"整型%i",intVa); //NSRange,结构体,不是一个对象,不需要加*号,表示1个范围。 NSRange range = NSMakeRange(5, 3); NSLog(@"range=%@",NSStringFromRange(range)); //字串查找 //查找字串所在的索引 range = [@"abcdefg" rangeOfString:@"cde"]; NSLog(@"range=%@",NSStringFromRange(range)); //截取从第4位开始的字符串 NSString *ab = @"abcdefg"; NSLog(@"%@",[ab substringFromIndex:4]); //截取到第4位的字符串 NSString *bc = @"abcdefg"; NSLog(@"%@",[bc substringToIndex:4]); //截取1个范围内的字符串 NSString *cd = @"abcdefg"; NSLog(@"%@",[cd substringWithRange:range]); //字串是否以xx作为前缀 NSString *df = @"abcdefg"; NSLog(@"%i",[df hasPrefix:@"abc"]); //字串是否以xx作为后缀 NSString *fd = @"abcdefg"; NSLog(@"%i",[fd hasSuffix:@"ef"]); //字串是否包含此字串 NSString *dg = @"abcdefg"; NSLog(@"%i",[dg containsString:@"fe"]); //比较2个字串是否相同 NSString *gh = @"abcdefg"; NSString *hi = @"abcdefg"; NSLog(@"%i",[gh isEqualToString:hi]); //拆分字串 NSString *hk = @"abcde|fg"; NSArray *array = [hk componentsSeparatedByString:@"|"]; NSLog(@"array=%@",array); //NSMutableString, 内容可变字符串 NSMutableString *mutableStr = [NSMutableString stringWithString:@"小明"]; NSLog(@"mutableStr=%@",mutableStr); mutableStr = @"小张"; NSLog(@"mutableStr=%@",mutableStr); } return 0;}
阅读全文
0 0
- objective-c 编程基础(3.4 Foundation框架之字符串)
- objective-c 编程基础(3.6 Foundation框架之数组)
- objective-c 编程基础(3.7 Foundation框架之字典)
- objective-c 编程基础(3.4 Foundation框架之数字对象)
- Objective C Foundation基础框架
- Objective C Foundation基础框架
- Objective-C之Foundation框架
- Objective-C之Foundation框架
- Objective-C Foundation框架 字符串、集合
- Objective-c 基础框架(Foundation Framework)
- Objective-C之Foundation框架的NSMutableDictionary
- Objective-C之Foundation框架--数据存储
- Objective-C---Foundation框架
- Objective-C Foundation框架
- Objective-C Foundation基础
- Objective C Foundation框架(NSNumber)
- Objective C Foundation框架(NSString)
- objective-c之Foundation
- 多层线性模型(HLM)--层次嵌套样本应用HLM的必然性论证
- 前端技术学习(二)js判断文件大小(兼容IE8及以上)
- 实现一个简单的工作流例子全过程
- websocket实现android消息推送
- 精心积累技术汇集
- objective-c 编程基础(3.4 Foundation框架之字符串)
- 简单sql注入实战
- 2017-2018 ACM-ICPC Southeast Regional Contest (Div. 1) F.Move Away 几何
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; che
- simulink模块库的一些介绍
- 浅谈CSRF
- WebSocket实现Android客户端之间的简单通讯
- webstorm 2017 激活破解
- 【java学习准备2】servlet是线程安全的吗?