美文网首页
iOS 字符串操作全解

iOS 字符串操作全解

作者: 孙健会员 | 来源:发表于2017-05-24 11:18 被阅读6次
c语言转换为OC
UTF8String
系统的静态方法不需要自己管理内存
1.将字符转换为大写
[@"Hello world!" uppercaseString];
2.将字符串小写
[@"Hello World!" lowercaseString];
3.将首字母大写
[@"Hello World!" capitalizedString];
4.字符串比较
[@"abc" isEqualToString:@"aBc"];
NSComparisonResult result = [@"abc" compare:@"aBc"];
NSComparisonResult result1 = [@"abc" caseInsensitiveCompare:@"aBc"];//忽略大小写
NSOrdereAscending(升序)
NSOrderedDescending(降序)
NSOrderedSame(相等)
5.开头
[@"abcdef" hasPrefix:@"ab"];
6.结尾
[@"abcdef" hanSuffix:@"ed"];
7.包含
NSRange range = [@"abcedfg" rangeofString:@"cde"];
range.location == NSNotFound
8.range转string
NSStringFromRange(range);
9.index到最后
[@"abcdefg" substringFromIndex:3];
10.开头到index
[@"abcdefg" substringToIndex:3];
11.截取
[@"abcdefd" substringWithRange:NSMakeRange(2,3)];
12.字符串分割
[@"abc.def.gh" componentsSeparatedByString:@"."];
13.长度
[@"abc" length];
14.某位
[@"abc" characterAtIndex:0];
15.日期
NSDate *date = [NSDate date];
16.在当前的日期上加上100秒
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:100];
17.随机获得将来的某一个时间
NSDate *date = [NSDate distantFuture];
18.日期转换为字符串
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
formater.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *dateStr = [format stringWithDate:date];
19. 字符串转换为日期
NSDate *date = [formater dateFromString:@"14-04-21 12:07:11"];

相关文章

网友评论

      本文标题:iOS 字符串操作全解

      本文链接:https://www.haomeiwen.com/subject/zndztxtx.html