//如何创建字符串对象
//1.字符串常量
NSString *str = @"NSString";
//2.用一个存在的字符串创建一个新的字符串对象
NSString *str2 = [NSString stringWithString:str];
NSString *str22 = [[NSString alloc] initWithString:str];
//3.按照一定的格式拼接字符串
int age = 20;
int score = 98;
NSString *str3 = [NSString stringWithFormat:@"xiaoming's age is %d score is %d",age, score];
NSString *str33 = [[NSString alloc] initWithFormat:@"xiaoming's age is %d score is %d",age, score];
NSLog(@"%@", str3);
//获取字符串长度 length
NSLog(@"length is :%lu",str3.length);
//比较两个字符串是否相同/大小
BOOL result = [str2 isEqualToString:str3];
if (result == YES) {
NSLog(@"the same");
} else {
NSLog(@"not the same");
}
NSComparisonResult cmpResult = [@"abcd" compare:@"abc"];
if (cmpResult == NSOrderedAscending) {
NSLog(@"ascending");
} else if(cmpResult == NSOrderedSame){
NSLog(@"the same");
} else {
NSLog(@"descending");
}
//字符串查找
NSString *sTemp1 = @"hello world";
NSLog(@"%@", [sTemp1 substringFromIndex:3]);
NSLog(@"%@", [sTemp1 substringToIndex:3]);
NSRange range = NSMakeRange(3, 4);
NSLog(@"%@", [sTemp1 substringWithRange:range]);
//查找子字符串
NSRange sRange = [sTemp1 rangeOfString:@"lo w"];
if (sRange.length != 0) {
NSLog(@"exist location:%lu",sRange.location);
} else {
NSLog(@"not exist");
}
//前缀后缀
NSString *httpStr = @"http://www.baidu.com";
if ([httpStr hasPrefix:@"http:"]) {
NSLog(@"YES");
} else {
NSLog(@"NO");
}
if ([httpStr hasSuffix:@".com"]) {
NSLog(@"YES");
} else {
NSLog(@"NO");
}
//字符串的数字 转化为 对应的类型
NSString *numStr = @"123";
int num = [numStr intValue];
NSLog(@"%d", num);
//NSString-imutable不可变字符串
//NSMutableString-mutable可变字符串
NSMutableString *orgStr = [NSMutableString stringWithString:@"Guizhou caijing university"];
//替换字符串
[orgStr replaceCharactersInRange:NSMakeRange(8, 7) withString:@"guida"];
NSLog(@"%@", orgStr);
//插入字符
[orgStr insertString:@"big " atIndex:0];
NSLog(@"%@", orgStr);
//追加字符串
[orgStr appendString:@"OK"];
NSLog(@"%@", orgStr);
[orgStr setString:@"xiaohuang"];
NSLog(@"%@", orgStr);
二.NSArray
/*
NSArray
创建数组,数组里面只能添加对象类型,不能添加c语言的基本类型
oc里面数组可以存放多种类型的对象 约定,在一个数组里面只存放一种类型,避免造成错误
Person *xh=[Person new];
1.@[]
NSArray *array1=@[@"hello",@"nihao",xh]
2,创建一个拥有一个对象的数组
NSArray *array2 = [NSArray arrayWithObject:@"nihao"]
3,创建一个拥有多个对象的数组,对象之间用逗号隔开,nil表示初始化结束,
在数组里不能添加值为nil的对象
*/
NSArray *array3 = [NSArray arrayWithObjects:@"nihao",@"wohao",@"123",nil];
//获取元素数组个数
NSLog(@"array3 count is %lu",[array3 count]);
//访问数组元素 objectAtIndex
NSString *str = [array3 objectAtIndex:1];
//获取某个对象在数组里面的索引值,不存在如何操作
int index = [array3 indexOfObject:@"wang"];
NSLog(@"%d",index);
for (int i=0; i<array3.count; i++) {
NSLog(@"%@",[array3 objectAtIndex:i]);
}
//for...in快速枚举法
for(NSString *temp in array3){
NSLog(@"%@",temp);
}
//字符串分割
//www baidu com
//componetsSeparatedByString:@".";
NSArray *comArray =@[@"www",@"baidu",@"com"];
NSString *httpStr = [comArray componentsJoinedByString:@"."];
NSLog(@"%@",httpStr);
//判断数组是否包含某个对象
BOOL result4 = [array3 containsObject:@"abc"];
//NSMutableArray 可变数组
//创建一个可变数组 数组里面初始状态可以容纳26个对象
//初始为26个,如元素不足26,则剩余空下存储空间,如超过26个对象,则自动增加个数
NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:26];
//添加对象 末尾添加
[mArray addObject:@"university"];
NSLog(@"%@",mArray);
//插入对象
[mArray insertObject:@"guizhou" atIndex:0];
NSLog(@"%@",mArray);
[mArray insertObject:@"finance" atIndex:1];
NSLog(@"%@",mArray);
[mArray insertObject:@"economics" atIndex:0];
NSLog(@"%@",mArray);
//删除对象
[mArray removeObjectAtIndex:2];
NSLog(@"%@",mArray);
//删除最后一个对象
[mArray removeLastObject];
NSLog(@"%@",mArray);
//替换对象
[mArray replaceObjectAtIndex:0 withObject:@"guizhou"];
NSLog(@"%@",mArray);
//交换位置
[mArray exchangeObjectAtIndex:0 withObjectAtIndex:1];
NSLog(@"%@",mArray);
//删除所有对象
[mArray removeAllObjects];
NSLog(@"%@",mArray);
三.NSDictionary
//NSString NSArray NSDictionary
//1.创建字典 key:object
//key:字符集
//value:object
NSDictionary *dic = @{@"one":@"1",@"two":@"2"};
//2.
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"2",@"two",@"3",@"three", nil];
// NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"2":@"wo", nil]
//3.获取字典键值对的个数
int num = [dic2 count];//dic2.count
/*
4.获得某个键对应的值,没有就返回nil
c语言表示空,NULL
oc表示空,nil,NULL,NSNULL
NULL 给c语言的指针类型
nil给oc的对象类型
NSNULL 向oc的数组里面添加一个值为空的对象(不常用)
*/
NSString *vStr = [dic2 objectForKey:@"three"];
NSLog(@"%@",vStr);
//5.获取所有的键
NSArray *keysArray = [dic2 allKeys];
for (NSString *key in keysArray) {
NSLog(@"value for %@ is %@",key,[dic2 objectForKey:key]);
}
//6.获取所有的值
NSArray *valueArray = [dic2 allValues];
//7.可变字典
NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithCapacity:0];
//(1)添加键值对
[mDic setObject:@"ahuang" forKey:@"xwName"];
[mDic setObject:@"bhuang" forKey:@"xw"];
//(2)删除键值对
[mDic removeObjectForKey:@"xw"];
[mDic removeAllObjects];