一卿樱、字符串
1销部、不可變字符串的創(chuàng)建(NSSString)
//最簡單的創(chuàng)建方式
NSString * str1 = @"I love iOS";
//以str1創(chuàng)建str2
NSString * str2 = [[NSString alloc] initWithString:str1];
//以C的字符串創(chuàng)建OC字符串
char * cStr = "I love iOS ha";
NSString * str3 = [[NSString alloc] initWithCString:cStr encoding:NSUTF8StringEncoding];
NSString * str4 = [[NSString alloc] initWithUTF8String:cStr];
//format格式創(chuàng)建字符串(拼接字符串)
int a = 1;
float b = 3.14;
NSString * str5 = @"I like iOS";
NSString * str6 = [[NSString alloc] initWithFormat:@"??%d??%f??%@",a,b,str6];
//從文件當中讀取字符串
NSString * str7 = [[NSString alloc] initWithContentsOfFile:PATH encoding:NSUTF8StringEncoding error:nil];
2、字符串的比較
//*****************C字符串的比較
char *p = "jos";
char *q = "ios";
int ret = strcmp(p, q);//ret = p - q
if (ret > 0) {
NSLog(@"p > q");
} else if (ret < 0) {
NSLog(@"p < q");
} else {
NSLog(@"p == q");
}
//*****************比較相等與否
NSString * str1 = @"I love ios";
NSString * str2 = @"I love ios";
if ([str1 isEqualToString:str2]) {
NSLog(@"相等");
}else{
NSLog(@"不相等");
}
//*****************OC字符串比較大小
NSString * str3 = @"how do you do";
NSString * str4 = @"how do you do";
NSComparisonResult result = [str3 compare:str4];
if (result == NSOrderedSame) {
NSLog(@"str3 == str4");
}else if (result == NSOrderedAscending){
NSLog(@"str3 < str4");
}else if (result == NSOrderedDescending){
NSLog(@"str3 > str4");
}
//*****************不區(qū)分大小寫的比較
NSString * str5 = @"how are you";
NSString * str6 = @"HOW ARE YOU";
NSComparisonResult result1 = [str5 caseInsensitiveCompare:str6];
if (result1 == NSOrderedSame) {
NSLog(@"str5 == str6");
}else if (result1 == NSOrderedAscending){
NSLog(@"str5 < str6");
}else if (result1 == NSOrderedDescending){
NSLog(@"str5 > str6");
}
3奥帘、字符串的處理
NSString * str = @"I love iOS";
//小寫轉大寫
NSString * str1 = [str uppercaseString];
//大寫轉小寫
NSString * str2 = [str lowercaseString];
//首字母大寫
NSString * str3 = [str capitalizedString];
//字符串范圍
NSRange range = [str rangeOfString:@"love"];//空格也要算在內
//位置range.location == 2铜邮,長度range.length == 4
//提取字符串(第一個字符的下標是0)
NSString * str4 = [str substringFromIndex:3];//截取從第3個字符開始到結尾(即:ove iOS)
NSString * str5 = [str substringToIndex:5];//截取前5個字符(即:I lov)
NSString * str6 = [str substringWithRange:NSMakeRange(2, 4)];//截取從第2個字符起往后數(shù)4個字符(即:love)
//判斷字符串首尾
if ([str hasPrefix:@"I love"]) {
NSLog(@"是以I love開頭");
}
if ([str hasSuffix:@"iOS"]) {
NSLog(@"是以iOS結尾");
}
4、可變字符串的創(chuàng)建(NSMutableString)
//方式1
NSString * str = @"I love iOS";
NSMutableString * mStr1 = [[NSMutableString alloc] initWithString:str];
//方式2
int i = 3;
NSMutableString * mstr2 = [[NSMutableString alloc] initWithFormat:@"i = %d",i];
//方式3
NSMutableString * mstr3 = [[NSMutableString alloc] initWithUTF8String:"haha"];
5寨蹋、可變字符串的處理
//字符串的追加1(追加到尾部)
NSMutableString * mStr1 = [[NSMutableString alloc] initWithString:@"I love iOS"];
[mStr1 appendString:@" very much"];
//結果:mStr1 = @"I love iOS very much"
//字符串的追加2(追加到尾部)
NSMutableString * mStr2 = [[NSMutableString alloc] initWithString:@"I love iOS"];
int i = 2;
[mStr2 appendFormat:@" ?%d", i];
//結果:mStr2 = @"I love iOS ?2?"
//字符串的插入
NSMutableString * mStr3 = [[NSMutableString alloc] initWithString:@"abcdefg"];
[mStr3 insertString:@"yyy" atIndex:3];
//結果:mStr3 = @"abcyyydefg"
//字符串的刪除
NSMutableString * mStr4 = [[NSMutableString alloc] initWithString:@"abcdefg"];
[mStr4 deleteCharactersInRange:NSMakeRange(3, 2)];
//結果:mStr4 = @"abcfg"
//字符串的替換
NSMutableString * mStr5 = [[NSMutableString alloc] initWithString:@"abcdefg"];
[mStr5 replaceCharactersInRange:NSMakeRange(0, 2) withString:@"1234"]
//結果:mStr5 = @"1234cdefg"
二松蒜、數(shù)組
1、不可變數(shù)組
#創(chuàng)建數(shù)組#
//創(chuàng)建方式1
NSArray *array1 = [[NSArray alloc] initWithObjects:@"xixi",@"haha",@"hehe", nil];
//創(chuàng)建方式2
NSArray *array2 = [[NSArray alloc] initWithArray:array1];
//創(chuàng)建方式3
NSArray *array3 = @[@"xixi",@"haha",@"hehe"];
//創(chuàng)建方式4
NSArray *array4 = [NSArray arrayWithObjects:@"how",@"are",@"you", nil];
//創(chuàng)建方式5
NSArray *array5 = [NSArray arrayWithArray:array4];
#查找元素位置#
NSArray *array6 = @[@"how", @"are", @"you"];
NSUInteger index = [array6 indexOfObject:@"are"];
//結果:index = 1
#字符串分割到數(shù)組里#
NSString *str = @"You@are@the@best";
NSArray *array7 = [str componentsSeparatedByString:@"@"];
//結果:array7 = @[@"You",@"are",@"the",@"best"];
#數(shù)組連接成字符串#
NSArray *array8 = @[@"I", @"love", @"you"];
NSString *str = [array8 componentsJoinedByString:@"?"];
//結果:str = @"I?love?you"
2已旧、可變數(shù)組
#創(chuàng)建數(shù)組#
//創(chuàng)建方式1
NSMutableArray * mArray1 = [[NSMutableArray alloc] initWithObjects:@"xixi",@"haha",@"hehe", nil];
//創(chuàng)建方式2
NSMutableArray * mArray2 = [[NSMutableArray alloc] initWithArray:mar1];
//創(chuàng)建方式3
NSMutableArray * mArray3 = [NSMutableArray arrayWithObjects:@"how",@"are",@"you", nil];
//創(chuàng)建方式4
NSMutableArray * mArray4 = [NSMutableArray arrayWithArray:mar3];
#增刪替換#
NSArray * array = @[@"how",@"do",@"you"];
NSMutableArray * mArray5 = [[NSMutableArray alloc] initWithArray:array];
//添加
[mArray5 addObject:@"do"];
//結果:mArray5 = @[@"how", @"do", @"you", @"do"]
//插入
[mArray5 insertObject:@"GG" atIndex:0];
//結果:mArray5 = @[@"GG", @"how", @"do", @"you", @"do"]
//刪除
[mArray5 removeObjectAtIndex:0];
//結果:mArray5 = @[@"how", @"do", @"you", @"do"]
[mArray5 removeLastObject];
//結果:mArray5 = @[@"how", @"do", @"you"]
//替換
[mArray5 replaceObjectAtIndex:0 withObject:@"what"];
//結果:mArray5 = @[@"what", @"do", @"you"]
//交換位置
[mArray5 exchangeObjectAtIndex:0 withObjectAtIndex:1]
//結果:mArray5 = @[@"do", @"what", @"you"]
三秸苗、字典
1、不可變字典
#創(chuàng)建字典#
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"one", @"1", @"two", @"2", @"three", @"3", nil];
#鍵值對的個數(shù)#
NSInteger count = [dict count];
#通過鍵查找值#
NSString *str = [dict objectForKey:@"two”];
NSString *str = dict[@"two”];
#遍歷#
for (NSString *str in dict) {
NSLog(@"%@", str); //遍歷的是字典dict中的鍵
NSLog(@"%@", dict[str]);//遍歷的是字典dict中的值
}
2运褪、可變字典
#創(chuàng)建可變字典#
NSMutableDictionary * mdict = [[NSMutableDictionary alloc] initWithDictionary:dict];
#增加鍵值對#
[mdict setObject:@"four" forKey:@"4"];
#刪除鍵值對#
[mdict5 removeObjectForKey:@“4”];//刪除單個鍵
[mdict5 removeAllObjects];//全部刪除
#字典替換#
NSDictionary * dict = @{@“3”:@“xixi", @"2":@"gogo"};
[mdict setDictionary:dict];//把字典mdict全部改為字典dict