分类: iOS平台
2015-04-02 23:16:54
int main(int argc, const char * argv[]) {
@autoreleasepool {
//大小写处理
NSString *s1 = @"abc";
//大写
NSLog(@"%@",[s1 uppercaseString]);
//小写
NSLog(@"%@",[s1 lowercaseString]);
//只有首字母变大写
NSLog(@"%@",[@"abCdE" capitalizedString]);
//字符串比较
//比较字符串内容是否一样
BOOL b = [@"abc" isEqualToString:@"abc"];
NSLog(@"%d",b);
//比较字符串大小
//NSOrderedAscending 右比左大, NSOrderedSame 一样 , NSOrderedDescending 右比左小
NSComparisonResult co = [@"abc" compare:@"ABC"];
if ( co == NSOrderedAscending) {
NSLog(@"升序");
}else if (co == NSOrderedSame){
NSLog(@"一样");
}else{
NSLog(@"降序");
}
//忽略大小写进行比较
[@"abc" caseInsensitiveCompare:@"Abc"];
//字符串的搜索
//是否已它开头
NSString *s2 = @"aqbc123";
NSLog(@"%d",[s2 hasPrefix:@"aq"]);
//是否已它结尾
NSLog(@"%d",[s2 hasSuffix:@"23"]);
//它在字符串的位置
NSRange ra = [s2 rangeOfString:@"c21"];
//ra.location == NSNotFound 没找到
if ( ra.length == 0) {
NSLog(@"没找到");
}else{
NSLog(@"%@",NSStringFromRange(ra));
}
//可以传入参数,前面还是后面搜索
//尾部开始搜索
[s2 rangeOfString:@"123" options:NSBackwardsSearch];
//字符串的截取
//从第4个开始截取
NSLog(@"%@",[s2 substringFromIndex:3]);
//截取到第4个,不包括4
NSLog(@"%@",[s2 substringToIndex:3]);
NSRange nsr = NSMakeRange(2, 3);
//从第三个开始截取3个
NSLog(@"%@",[s2 substringWithRange:nsr]);
//用数组装,进行分割
NSString *s3 = @"a-2-3-123-2";
NSArray *arr = [s3 componentsSeparatedByString:@"-"];
NSLog(@"%@",arr);
//访问数组
NSLog(@"%@",[arr objectAtIndex:2]);
//路径
//快速创建可变数组
NSMutableArray *n1 = [NSMutableArray array];
[n1 addObject:@"123"];
[n1 addObject:@"abc"];
//将数组的东西拼成路径
NSString *p1 = [NSString pathWithComponents:n1];
NSLog(@"%@",p1);
//路径分割成数组
NSArray *n2 = [p1 pathComponents];
NSLog(@"%@",n2);
//判断是否是绝对路径
NSString *p3 = @"/12/werw/2adsf";
NSLog(@"%d",[p3 isAbsolutePath]);
//返回最后一个目录名字
NSLog(@"%@",[p3 lastPathComponent]);
//删除最后一个目录
NSLog(@"%@",[p3 stringByDeletingLastPathComponent]);
//在最后拼接一个目录
NSLog(@"%@",[p3 stringByAppendingPathComponent:@"bbb"]);
//扩展名
NSString *p4 = @"123.txt";
NSString *p5 = @"mmm";
//获取扩展名
NSLog(@"%@",[p4 pathExtension]);
//删掉扩展名
NSLog(@"%@",[p4 stringByDeletingPathExtension]);
//拼接扩展名
NSLog(@"%@",[p5 stringByAppendingPathExtension:@"mp3"]);
//转换
NSString *si = @"1234";
int a = [si intValue];
NSLog(@"%d",a);
//计算字数,不是字符数
NSLog(@"%zd",[@"你好北京" length]);
//取字符
char c =[@"abcdsf" characterAtIndex:3];
NSLog(@"%c",c);
//返回C语言字符串
const char *ssss = [@"adad" UTF8String];
NSLog(@"%s",ssss);
}
return 0;
}