常見的NSString和NSMutableString方法:
api
NSString方法:
數組
- +(id) stringWithContentsOfFile:path encoding:enc error:err
- 建立一個新字符串並將其設置爲path指定的文件的內容,使用字符編碼enc,若是非零,則返回err中錯誤
-
- +(id) stringWithContentsOfURL:url encoding:enc error:err
- 建立一個新的字符串,並將其設置爲url的內容,使用字符編碼enc,若是非零,則返回err中的錯誤
-
- +(id) string
- 建立一個新的空字符串
-
- +(id) stringWithString:nsstring
- 建立一個新的字符串,並將其設置爲nsstring
-
- -(id)initWithString:nsstring
- 將分配的字符串設置爲nsstring
-
- -(id) initWithContentsOfFile:path encoding:enc error:err
- 將字符串設置爲path制定的文件的內容
-
- -(id) initWithContentsOfURL:url encoding:enc error:err
- 將字符串設置爲url(NSURL *)url的內容,使用字符編碼enc,若是非零,則返回err中的錯誤
-
- -(id) (UNSIgned int)length
- 返回字符串中的字符數目
-
- -(unichar)characterAtIndex:i
- 返回索引i的Unicode字符
-
- -(NSString *)substringFromIndex:i
- 返回從i開始知道結尾的子字符串
-
- -(NSString *)substringWithRange:range
- 根據指定範圍返回子字符串
-
- -(NSString *)substringToIndex:i
- 返回從該字符串開始到索i的子字符串
-
- -(NSComparator *)caseInsensitiveCompare:nsstring
- 比較兩個字符串,忽略大小寫
-
- -(NSComparator *)compare:nsstring
- 比較兩個字符串
-
- -(BOOL)hasPrefix:nsstring
- 測試字符串是否以nsstring開始
-
- -(BOOL)hasSuffix:nsstring
- 測試字符串是否以nsstrng結尾
-
- -(BOOL)isEqualToString:nsstring
- 測試兩個字符串是否相等
-
- -(NSString *) capitalizedString
- 返回每一個單詞首字母大寫的字符串(每一個單詞的其他字母轉換爲小寫)
-
- -(NSString *)lowercaseString
- 返回轉換爲小寫的字符串
-
- -(NSString *)uppercaseString
- 返回轉換爲大寫的字符串
-
- -(const char*)UTF8String
- 返回轉換爲UIF-8字符串的字符串
-
- -(double)doubleValue
- 返回轉換爲double的字符串
-
- -(float)floatValue
- 返回轉換爲浮點值的字符串
-
- -(NSInteger)integerValue
- 返回轉換爲NSInteger整數的字符串
-
- -(int)intValue
- 返回轉換爲整數的字符串
-
- NSMutableString方法
- +(id) stringWithCapacity:size
- 建立一個字符串,初始包含size的字符
-
- -(id) initWithCapacity:size
- 使用初始容量爲size的字符串來初始化字符串
-
- -(void) setString:nsstring
- 將字符串設置爲nsstring
-
- -(void) appendString:nsstring
- 在接收者的末尾附加nsstring
-
- -(void) deleteCharactersInRange:range
- 刪除指定range中的字符
-
- -(void) insertString:nsstring atIndex:i
- 以索引i爲起始位置插入nsstring
-
- -(void) replaceCharactersInRange:range withString:nsstring
- 使用nsstring替換range指定的字符
-
- -(void) replaceOccurrencesOf
- String:nsstring withString:nsstring2 options:opts range:range
- 根 據選項opts。使用指定range中的nsstring2替換全部的nsstring。選項能夠包括NSBackwardsSearch(從範圍的結 尾 開始搜索)NSAnchoredSearch(nsstring必須匹配範圍的開始),NSLiteralSearch(執行逐字節比較以 及 NSCaceInsensitiveSearch的按位或組合)
NSArray的基本操做代碼:
app
- /*---------------------------建立數組------------------------------*/
- //NSArray *array = [NSArray alloc] initWithObjects:
- @"One",@"Two",@"Three",@"Four",nil];
-
- self.dataArray = array;
- [array release];
-
- //- (unsigned) Count;數組所包含對象個數;
- NSLog(@"self.dataArray cound:%d",[self.dataArray count]);
-
- //- (id) objectAtIndex: (unsigned int) index;獲取指定索引處的對象;
- NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]);
-
-
- /*--------------------------從一個數組拷貝數據到另外一數組(可變數級)----------------------------*/
-
- //arrayWithArray:
- //NSArray *array1 = [NSArray alloc] init];
- NSMutableArray *MutableArray = [NSMutableArray alloc] init];
- NSArray *array = [NSArray arrayWithObjects:
- @"a",@"b",@"c",nil];
- NSLog(@"array:%@",array);
- MutableArray = [NSMutableArray arrayWithArray:array];
- NSLog(@"MutableArray:%@",MutableArray);
-
- array1 = [NSArray arrayWithArray:array];
- NSLog(@"array1:%@",array1);
-
-
- //Copy
-
- //id obj;
- NSMutableArray *newArray = [NSMutableArray alloc] init];
- NSArray *oldArray = [NSArray arrayWithObjects:
- @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
-
- NSLog(@"oldArray:%@",oldArray);
- for(int i = 0; i < [oldArray count]; i++)
- {
- obj = [oldArray objectAtIndex:i] copy];
- [newArray addObject: obj];
- }
- //
- NSLog(@"newArray:%@", newArray);
- [newArray release];
-
-
- //快速枚舉
-
- //NSMutableArray *newArray = [NSMutableArray alloc] init];
- NSArray *oldArray = [NSArray arrayWithObjects:
- @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
- NSLog(@"oldArray:%@",oldArray);
-
- for(id obj in oldArray)
- {
- [newArray addObject: obj];
- }
- //
- NSLog(@"newArray:%@", newArray);
- [newArray release];
-
-
- //Deep copy
-
- //NSMutableArray *newArray = [NSMutableArray alloc] init];
- NSArray *oldArray = [NSArray arrayWithObjects:
- @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
- NSLog(@"oldArray:%@",oldArray);
- newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);
- NSLog(@"newArray:%@", newArray);
- [newArray release];
-
-
- //Copy and sort
-
- //NSMutableArray *newArray = [NSMutableArray alloc] init];
- NSArray *oldArray = [NSArray arrayWithObjects:
- @"b",@"a",@"e",@"d",@"c",@"f",@"h",@"g",nil];
- NSLog(@"oldArray:%@",oldArray);
- NSEnumerator *enumerator;
- enumerator = [oldArray objectEnumerator];
- id obj;
- while(obj = [enumerator nextObject])
- {
- [newArray addObject: obj];
- }
- [newArray sortUsingSelector:@selector(compare:)];
- NSLog(@"newArray:%@", newArray);
- [newArray release];
-
-
-
- /*---------------------------切分數組------------------------------*/
-
- //從字符串分割到數組- componentsSeparatedByString:
- NSString *string = [NSString alloc] initWithString:@"One,Two,Three,Four"];
- NSLog(@"string:%@",string);
- NSArray *array = [string componentsSeparatedByString:@","];
- NSLog(@"array:%@",array);
- [string release];
-
-
- //從數組合並元素到字符串- componentsJoinedByString:
- NSArray *array = [NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
- NSString *string = [array componentsJoinedByString:@","];
- NSLog(@"string:%@",string);
-
-
-
- /*******************************************************************************************
- NSMutableArray
- *******************************************************************************************/
- /*---------------給數組分配容量----------------*/
- //NSArray *array;
- array = [NSMutableArray arrayWithCapacity:20];
-
-
-
- /*--------------在數組末尾添加對象----------------*/
- //- (void) addObject: (id) anObject;
- //NSMutableArray *array = [NSMutableArray arrayWithObjects:
- @"One",@"Two",@"Three",nil];
- [array addObject:@"Four"];
- NSLog(@"array:%@",array);
-
-
-
- /*--------------刪除數組中指定索引處對象----------------*/
- //-(void) removeObjectAtIndex: (unsigned) index;
- //NSMutableArray *array = [NSMutableArray arrayWithObjects:
- @"One",@"Two",@"Three",nil];
- [array removeObjectAtIndex:1];
- NSLog(@"array:%@",array);
-
-
-
- /*-------------數組枚舉---------------*/
- //- (NSEnumerator *)objectEnumerator;從前向後
- //NSMutableArray *array = [NSMutableArray arrayWithObjects:
- @"One",@"Two",@"Three",nil];
- NSEnumerator *enumerator;
- enumerator = [array objectEnumerator];
-
- id thingie;
- while (thingie = [enumerator nextObject]) {
- NSLog(@"thingie:%@",thingie);
- }
-
-
- //- (NSEnumerator *)reverseObjectEnumerator;從後向前
- //NSMutableArray *array = [NSMutableArray arrayWithObjects:
- @"One",@"Two",@"Three",nil];
- NSEnumerator *enumerator;
- enumerator = [array reverseObjectEnumerator];
-
- id object;
- while (object = [enumerator nextObject]) {
- NSLog(@"object:%@",object);
- }
-
-
- //快速枚舉
- //NSMutableArray *array = [NSMutableArray arrayWithObjects:
- @"One",@"Two",@"Three",nil];
- for(NSString *string in array)
- {
- NSLog(@"string:%@",string);
- }
-
-
-
- /*******************************************************************************************
- NSDictionary
- *******************************************************************************************/
-
- /*------------------------------------建立字典------------------------------------*/
- //- (id) initWithObjectsAndKeys;
-
- //NSDictionary *dictionary = [NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];
- NSString *string = [dictionary objectForKey:@"One"];
- NSLog(@"string:%@",string);
- NSLog(@"dictionary:%@",dictionary);
- [dictionary release];
-
-
- /*******************************************************************************************
- NSMutableDictionary
- *******************************************************************************************/
-
- /*------------------------------------建立可變字典------------------------------------*/
- //建立
- NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
-
- //添加字典
- [dictionary setObject:@"One" forKey:@"1"];
- [dictionary setObject:@"Two" forKey:@"2"];
- [dictionary setObject:@"Three" forKey:@"3"];
- [dictionary setObject:@"Four" forKey:@"4"];
- NSLog(@"dictionary:%@",dictionary);
-
- //刪除指定的字典
- [dictionary removeObjectForKey:@"3"];
- NSLog(@"dictionary:%@",dictionary);
-
-
- /*******************************************************************************************
- NSValue(對任何對象進行包裝)
- *******************************************************************************************/
-
- /*--------------------------------將NSRect放入NSArray中------------------------------------*/
- //將NSRect放入NSArray中
- NSMutableArray *array = [NSMutableArray alloc] init];
- NSValue *value;
- CGRect rect = CGRectMake(0, 0, 320, 480);
- value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)];
- [array addObject:value];
- NSLog(@"array:%@",array);
-
- //從Array中提取
- value = [array objectAtIndex:0];
- [value getValue:&rect];
- NSLog(@"value:%@",value);
-
-
- /*******************************************************************************************
- 從目錄搜索擴展名爲jpg的文件
- *******************************************************************************************/
-
- //NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *home;
- home = @"../Users/";
-
- NSDirectoryEnumerator *direnum;
- direnum = [fileManager enumeratorAtPath: home];
-
- NSMutableArray *files = [NSMutableArray alloc] init];
-
- //枚舉
- NSString *filename;
- while (filename = [direnum nextObject]) {
- if([filename pathExtension] hasSuffix:@"jpg"]){
- [files addObject:filename];
- }
- }
-
- //快速枚舉
- //for(NSString *filename in direnum)
- //{
- // if([filename pathExtension] isEqualToString:@"jpg"]){
- // [files addObject:filename];
- // }
- //}
- NSLog(@"files:%@",files);
-
- //枚舉
- NSEnumerator *filenum;
- filenum = [files objectEnumerator];
- while (filename = [filenum nextObject]) {
- NSLog(@"filename:%@",filename);
- }
-
- //快速枚舉
- //for(id object in files)
- //{
- // NSLog(@"object:%@",object);
- //}