比较前置串和后置串
NSString
-(BOOL)hasPrefix:(NSString *)string
当字符串中含有前置串string相等时返回YES,否则返回NO
-(BOOL)hasSuffix:(NSString *)string
当字符串中含有前置串string相等时返回YES,否则返回NO
例子:
NSString *str1 = @"Mac pro";
NSString *str2 = @"Mac os";
BOOL result = [str1 hasPrefix:@"Mac]; //YES
BOOL result1 = [str2 hasPrefix:@"Mac];//YES
BOOL result3 = [str1 hasSuffix:@"os]; //NO
BOOL result4 = [str2 hasSuffix:@"os]; //YES
遍历数组
1.常规方法
NSArray *array=[NSArray arrayWithObjects:
@"aa",@"bb",@"cc",@"dd", nil];
NSUInteger length = [array count];
for(int i = 0; i<length;i++){
NSString *elements = [array objectAtIndex:i];
NSLog(@"%@",elements);
}
2.快速枚举
for(NSString *string in array){
NSLog(@"%@",string);
}
字典的初始化
NSNumber *number = [NSNumber numberWithInt:100];
NSDictionary *dic = [NSDictionary dictionaryWithObject:number forKey:@"key"];
NSLog(@"%@",dic);
NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"valuekey",@"valueskey1", nil];
NSLog(@"%@",dic1);
NSDictionary *dic2 = [NSDictionary dictionaryWithDictionary:dic];
NSLog(@"%@",dic2);
NSInteger count = [dic count];
NSLog(@"%ld",count);
//通过key获取对应的value的对象
NSObject *valueObj = [dic objectForKey:@"key"];
NSLog(@"%@",valueObj);
//将字典的key转成一个枚举对象,用于遍历
NSEnumerator *enumerator = [dic keyEnumerator];
NSLog(@"%@",enumerator);
//获取所有键的集合
NSArray *keys = [dic allKeys];
//获取所有值的集合
NSArray *values = [dic allValues];
NSLog(@"%@",keys);
NSLog(@"%@",values)
运行结果:
2014-03-03 20:10:22.242 NS_Practice[465:303] {
key = 100;
}
2014-03-03 20:10:22.251 NS_Practice[465:303] {
valueskey1 = valuekey;
}
2014-03-03 20:10:22.271 NS_Practice[465:303] {
key = 100;
}
2014-03-03 20:10:22.274 NS_Practice[465:303] 1
2014-03-03 20:10:22.277 NS_Practice[465:303] 100
2014-03-03 20:22:52.729 NS_Practice[475:303] (
key
)
2014-03-03 20:22:52.733 NS_Practice[475:303] (
100
)
Program ended with exit code: 0
objecti-c中的 比较前置串和后置串, 遍历数组和字典的用法,布布扣,bubuko.com
objecti-c中的 比较前置串和后置串, 遍历数组和字典的用法
原文:http://blog.csdn.net/eduora_meimei/article/details/20400195