// Create By 郭仔 2015年03月31日20:54:20
1. NSString類
// 求字符串長度
NSString *str = @"Hello郭String";
NSUInteger len = [str length]; // NSUInterger即long
NSLog(@"%ld",len);
//獲取子字符串
NSString *substr = [str substringFromIndex:5];//從下標5開始取子字符串
NSLog(@"%@",substr);
// 從開始取子字符串到下標為5的位置截止
NSString *substr2 = [str substringToIndex:5];
NSLog(@"%@",substr2);
NSRange rang = {2,3}; //起始位置和長度
// 從起始位置2取長度為3的子字符串
NSString *substr3 = [str substringWithRange:rang];
NSLog(@"%@",substr3);
//拼接字符串
NSString *comStr1 = [str stringByAppendingString:@"IOS"];
NSLog(@"%@",comStr1);
NSString *comStr2 = [str stringByAppendingFormat:@"hehe%@ %@",@"hhh",@"jjj"];
NSLog(@"%@",comStr2);
//字符串替換
NSString * newStr1 = [str stringByReplacingOccurrencesOfString:@"string" withString:@"world"];
NSLog(@"%@",newStr1);
// 判斷字符串是否相等
//判斷字符串是否相等用 isEqualTo方法,不能用==判斷
// isEqualTo方法判斷的時字符串的內容是否相等,而==判斷的時兩個指針是否指向同一地址
NSString *cmpStr = @"Hello String";
BOOL eq = [str isEqualToString:cmpStr];
if (eq) {
NSLog(@"相等");
}
else{
NSLog(@"不相等");
}
// 判斷前綴是否已給定的字符串相等,即是否已該字符串開頭
BOOL prefix = [str hasPrefix:@"Hello"];
if (prefix) {
NSLog(@"以Hello開頭");
}
else {
NSLog(@"不以Hello開頭");
}
//判斷後綴
//判斷是否已png結尾,是就替換成jpg,否則拼接jpg
BOOL isPng = [str hasSuffix:@"png"];
if (isPng) {
//執行替換
NSString * tem = [str stringByReplacingOccurrencesOfString:@"png" withString:@"jpg"];
NSLog(@"%@",tem);
}
else
{ //執行拼接jpg
NSString *tem = [str stringByAppendingString:@"jpg"];
NSLog(@"%@",tem);
}
=====================================================================================
2. NSMutableString//可變字符串NSMutableString *mulStr = [NSMutableString stringWithString:@"Hello"];
NSLog(@"%@",mulStr);
//拼接
[mulStr appendFormat:@"guozai"];
NSLog(@"%@",mulStr);
[mulStr appendString:@"mutible"];
NSLog(@"%@",mulStr);
//刪除子字符串
NSRange ran = {4,1};//結構體類型
[mulStr deleteCharactersInRange:ran];
NSLog(@"%@",mulStr);
//替換
NSRange ran2 = {3,2};
[mulStr replaceCharactersInRange:ran2 withString:@"yy"];
NSLog(@"%@",mulStr);
//插入
[mulStr insertString:@"tt" atIndex:2];
NSLog(@"%@",mulStr);
// 對於不可變字符串NSString的字符串拼接,分割等操作,都會創建新的字符串
// 對於可變字符串NSMutableString的字符串拼接分割替換等操作是在原字符串的基礎上
// 進行修改,不會創建新字符串
// NSMutableString是NSString的子類,所以NSString的方法,NSMutableString也
//可以使用
// 在以後的學習中,凡是出現Mutable的類,都是不帶Mutable類的子類:
//如,NSMutableArray是NSArray的子類,NSMutableDictionary是NSDictonary的子
//類
==================================================================================== 3.NSArray
//數組
//最後的nil不可丟掉
NSArray *arr =[NSArray arrayWithObjects:@"guozai", @"guo",@"zaiguo",nil];
// 獲取數組元素個數
NSUInteger count = [arr count];
NSLog(@"%lu",count);
//獲取第一個對象
NSString *p1 = [arr firstObject];
NSLog(@"%@",p1);
// 獲取最後一個對象
NSString *p2 = [arr lastObject];
NSLog(@"%@",p2);
// 獲取下標對應的對象
NSString *p3 = [arr objectAtIndex:1];
NSLog(@"%@",p3);
//遍歷數組
for (int i = 0; i < [arr count]; i++) {
NSLog(@"%@",[arr objectAtIndex:i]);
}
===================================================================================== 4.NSMutableArray
//可變數組
// 一個數組的內容賦給另一個數組
NSMutableArray * mulArray = [NSMutableArray arrayWithArray:arr];
//刪除下標為index的對象
[mulArray removeObjectAtIndex:2];
// for (int i = 0; i < [arr count]; i++) {
// NSLog(@"%@",[arr objectAtIndex:i]);
// }
//
// 添加一個對象元素
[mulArray addObject:@"guoguo"];
// 交換下標對應的元素對象
[mulArray exchangeObjectAtIndex:0 withObjectAtIndex:[mulArray count]-1]; //交換第一個元素和最後一個元素
===================================================================================== 下面通過一個實例來形象的了解: 使用可變數組管理BOOk類,實現圖書的增刪查改 BOOK有兩個成員變量:_name,_price;Book * book1 = [[Book alloc] initWithName:@"guozai1" andPrice:10];
Book * book2 = [[Book alloc] initWithName:@"guozai2" andPrice:15];
Book * book3 = [[Book alloc] initWithName:@"guozai3" andPrice:13];
//數組賦值
NSMutableArray *books = [NSMutableArray arrayWithObjects:book1,book2,book3, nil];
Book * book4 = [[Book alloc] initWithName:@"guozai4" andPrice:12];
//添加一本書
[books addObject:book4];
//刪除一本書
[books removeObjectAtIndex:2];
for (int i = 0; i < [books count]; i ++) {
NSLog(@"%@,%.2f",[[books objectAtIndex:i] name],[[books objectAtIndex:i] price]);
}
//查找名字是guozai3的書,打印價格
for (int i = 0; i < [books count]; i ++) {
if ([[[books objectAtIndex:i] name] isEqualToString:@"guozai3"]) {
NSLog(@"%f",[[books objectAtIndex:i] price]);
}
}
// 對數組進行排序,按價格從高到低
for (int i = 0; i < [books count] - 1; i ++) {
for (int j = 0; j < [books count] - i - 1; j ++) {
if ([books[j] price] < [books[j+1] price]) {
[books exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
for (int i = 0; i < [books count]; i ++) {
NSLog(@"%@,%.2f",[books[i] name],[books[i] price]);
}
======================================================================================== 5.NSNumber:將基本數據類型轉化成對象類型//將基本數據類型int轉化為對象類型
NSNumber * intNum = [NSNumber numberWithInt:100];//便利構造器
NSMutableArray * ar = [NSMutableArray arrayWithObjects:intNum, nil];
NSNumber * tem = [ar objectAtIndex:0];
//將對象類型轉化成基本數據類型
int result = [tem intValue];
NSLog(@"%d",result);
======================================================================================== 6.NSValue:將結構體轉化成對象//將一個點轉化成NSValue對象
NSPoint point = {1,2};
//將一個結構體轉化成NSValue對象
NSValue *vPoint = [NSValue valueWithPoint:point];
//將vPoint轉化成結構體
NSPoint point2 = [vPoint pointValue];
// NSLog(@"%.2f,%.2f",point2.x,point2.y);
// NSStringFromPoint可以將點轉化成字符串
NSLog(@"%@",NSStringFromPoint(point2));
//將NSsize結構體轉化成NSValue對象
NSSize size = {22,44};
NSValue * sValue = [NSValue valueWithSize:size];
//將NSValue對象轉化成NSSize結構體;
NSSize size2 = [sValue sizeValue];
NSLog(@"%@", NSStringFromSize(size2));
========================================================================================