字符串操作
1
2
3
4
5
6
7
8
9
10
11
12
|
/*-----字符串常用操作-------*/ NSLog (@ "rect1: %@" , NSStringFromCGRect (rect1));
//1,字符串拼接
NSString
*newString = [ NSString
stringWithFormat:@ "%@%@" ,tempA,tempB];
//2,字符转int
int
intString = [newString intValue];
//3,int转字符
NSString
*stringInt = [ NSString
stringWithFormat:@ "%d" ,intString];
//4,字符转float
float
floatString = [newStringfloatValue];
//5,float转字符
NSString
*stringFloat = [ NSStringstringWithFormat :@ "%f" ,intString];
|
1 /*-------创建一个数组,并初始化数组为输入值,然后写入-----------*/ 2 NSDictionary *RegisterDic_ = [[NSDictionaryalloc] initWithObjects: 3 [NSArrayarrayWithObjects:_userNameTextField.text,_passwordTextField.text,_emailTextField.text, nil] 4 forKeys: [NSArrayarrayWithObjects:@"username",@"password",@"email",nil]]; 5 6 NSString * homebrew = @"imperial india pale Ale(ipa)"; 7 //从第25开始,获取三个字符 8 NSRange range =NSMakeRange(25, 3); 9 //以下代码等价 10 // NSRange range ={25,3}; 11 //对字符串取子串 12 NSLog(@"beer shortname %@",[homebrew substringWithRange:range]); 13 14 //搜索字符串,输出range is{25,3} 15 NSRange range = [homebrew rangeOfString:@"ipa"]; 16 //判断是否找到字符串 17 if (range.length]] > 0) 18 { 19 NSLog(@"%@",NSStringFromRect(range)); 20 } 21 22 23 在iphone中,除了系统的颜色之外,我们还可以自定义颜色,下面有两种方式: 24 UIColor *newColor = [[UIColor alloc] initWithRed:(float) green:(float) blue:(float) alpha:(float)]; 25 26 //alpha: 0.0~1.0 27 28 UIColor *tiniColor = 29 30 [UIColor colorWithRed:255/255.0 green:175/255.0 blue:64/255.0 alpha:1.0]; 31 32 修改navigationbar的背景颜色 33 34 35 可以修改navigationbar的tintcolor属性来达到目的, 36 37 self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:200/255.0f green:200/255.0f blue:184/255.0f alpha:1.0f]; 38 /******************************************************************************************* 39 NSString 40 *******************************************************************************************/ 41 //一、NSString 42 /*----------------创建字符串的方法----------------*/ 43 //获取文件路径 NSString *path =[ [NSBundle mainBundle] pathForResource:@"test"ofType:@"png"]; 44 NSFileManager *fm = [NSFileManagerdefaultManager]; 45 if([fm fileExistsAtPath:path]) 46 NSLog(@"yes"); 47 else 48 NSLog(@"no"); 49 50 //1、创建常量字符串。 51 NSString *astring = @"This is a String!"; 52 53 //2、创建空字符串,给予赋值。 54 55 NSString *astring = [[NSString alloc] init]; 56 astring = @"This is a String!"; 57 [astring release]; 58 NSLog(@"astring:%@",astring); 59 60 //3、在以上方法中,提升速度:initWithString方法 61 62 NSString *astring = [[NSString alloc] initWithString:@"This is a String!"]; 63 NSLog(@"astring:%@",astring); 64 [astring release]; 65 66 //4、用标准c创建字符串:initWithCString方法 67 68 char *Cstring = "This is a String!"; 69 NSString *astring = [[NSString alloc] initWithCString:Cstring]; 70 NSLog(@"astring:%@",astring); 71 [astring release]; 72 73 //5、创建格式化字符串:占位符(由一个%加一个字符组成) 74 75 int i = 1; 76 int j = 2; 77 NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]]; 78 NSLog(@"astring:%@",astring); 79 [astring release]; 80 81 //6、创建临时字符串 82 83 NSString *astring; 84 astring = [NSString stringWithCString:"This is a temporary string"]; 85 NSLog(@"astring:%@",astring); 86 87 /*----------------从文件读取字符串:initWithContentsOfFile方法----------------*/ 88 89 NSString *path = @"astring.text"; 90 NSString *astring = [[NSString alloc] initWithContentsOfFile:path]; 91 NSLog(@"astring:%@",astring); 92 [astring release]; 93 //打开指定路径上的文本文件,并使用文件内容初始化一个字符串; 94 /*----------------写字符串到文件:writeToFile方法----------------*/ 95 96 NSString *astring = [[NSString alloc] initWithString:@"This is a String!"]; 97 NSLog(@"astring:%@",astring); 98 NSString *path = @"astring.text"; 99 [astring writeToFile: path atomically: YES]; 100 [astring release]; 101 102 /*----------------比较两个字符串----------------*/ 103 104 //用C比较:strcmp函数 105 106 char string1[] = "string!"; 107 char string2[] = "string!"; 108 if(strcmp(string1, string2) = = 0) 109 { 110 NSLog(@"1"); 111 } 112 113 //isEqualToString方法 114 NSString *astring01 = @"This is a String!"; 115 NSString *astring02 = @"This is a String!"; 116 BOOL result = [astring01 isEqualToString:astring02]; 117 NSLog(@"result:%d",result); 118 119 //compare方法(comparer返回的三种值) 120 NSString *astring01 = @"This is a String!"; 121 NSString *astring02 = @"This is a String!"; 122 BOOL result = [astring01 compare:astring02] = = NSOrderedSame; 123 NSLog(@"result:%d",result); 124 //NSOrderedSame判断两者内容是否相同 125 126 NSString *astring01 = @"This is a String!"; 127 NSString *astring02 = @"this is a String!"; 128 BOOL result = [astring01 compare:astring02] = = NSOrderedAscending; 129 NSLog(@"result:%d",result); 130 //NSOrderedAscending判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真) 131 132 NSString *astring01 = @"this is a String!"; 133 NSString *astring02 = @"This is a String!"; 134 BOOL result = [astring01 compare:astring02] = = NSOrderedDescending; 135 NSLog(@"result:%d",result); 136 //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真) 137 138 //不考虑大小写比较字符串1 139 NSString *astring01 = @"this is a String!"; 140 NSString *astring02 = @"This is a String!"; 141 BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame; 142 NSLog(@"result:%d",result); 143 //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真) 144 145 //不考虑大小写比较字符串2 146 NSString *astring01 = @"this is a String!"; 147 NSString *astring02 = @"This is a String!"; 148 BOOL result = [astring01 compare:astring02 149 options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame; 150 NSLog(@"result:%d",result); 151 152 //NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。 153 154 /*----------------改变字符串的大小写----------------*/ 155 156 NSString *string1 = @"A String"; 157 NSString *string2 = @"String"; 158 NSLog(@"string1:%@",[string1 uppercaseString]);//大写 159 NSLog(@"string2:%@",[string2 lowercaseString]);//小写 160 NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小 161 162 /*----------------在串中搜索子串----------------*/ 163 164 NSString *string1 = @"This is a string"; 165 NSString *string2 = @"string"; 166 NSRange range = [string1 rangeOfString:string2]; 167 int location = range.location;//位置 168 int leight = range.length;//長度 169 NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]]; 170 NSLog(@"astring:%@",astring); 171 [astring release]; 172 /*----------------判断字符串里是否有此字符串----------------*/ 173 174 NSRange range = [fileName rangeOfString:@"."]; 175 if (range.length==0) 176 177 178 /*----------------抽取子串 ----------------*/ 179 180 //-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符 181 NSString *string1 = @"This is a string"; 182 NSString *string2 = [string1 substringToIndex:3]; 183 NSLog(@"string2:%@",string2); 184 185 //-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符 186 NSString *string1 = @"This is a string"; 187 NSString *string2 = [string1 substringFromIndex:3]; 188 NSLog(@"string2:%@",string2); 189 190 //-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串 191 NSString *string1 = @"This is a string"; 192 NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)]; 193 NSLog(@"string2:%@",string2); 194 195 196 //扩展路径 197 NSString *Path = @"~/NSData.txt"; 198 NSString *absolutePath = [Path stringByExpandingTildeInPath]; 199 NSLog(@"absolutePath:%@",absolutePath); 200 NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]); 201 202 //文件扩展名 203 NSString *Path = @"~/NSData.txt"; 204 NSLog(@"Extension:%@",[Path pathExtension]); 205 206 /******************************************************************************************* 207 NSMutableString 208 *******************************************************************************************/ 209 210 /*---------------给字符串分配容量----------------*/ 211 //stringWithCapacity: 212 NSMutableString *String; 213 String = [NSMutableString stringWithCapacity:40]; 214 //容量只是建议的最优值,而不是最大值 215 216 /*---------------在已有字符串后面添加字符----------------*/ 217 218 //appendString: and appendFormat: 219 220 NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; 221 //[String1 appendString:@", I will be adding some character"]; 222 [String1 appendFormat:[NSString stringWithFormat:@", I will be adding some character"]]; 223 NSLog(@"String1:%@",String1); 224 */ 225 226 /*--------在已有字符串中按照所给出范围和长度删除字符------*/ 227 /* 228 //deleteCharactersInRange: 229 NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; 230 [String1 deleteCharactersInRange:NSMakeRange(0, 5)]; 231 NSLog(@"String1:%@",String1); 232 // 233 [string1 deleteCharacterslnRange]:(NSRange *)range; 234 /*--------在已有字符串后面在所指定的位置中插入给出的字符串------*/ 235 236 //-insertString: atIndex: 237 NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; 238 [String1 insertString:@"Hi! " atIndex:0]; 239 NSLog(@"String1:%@",String1); 240 241 242 /*--------将已有的空符串换成其它的字符串------*/ 243 244 //-setString: 245 NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; 246 [String1 setString:@"Hello Word!"]; 247 NSLog(@"String1:%@",String1); 248 249 /*--------按照所给出的范围,和字符串替换的原有的字符------*/ 250 //-setString: 251 NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; 252 [String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"]; 253 NSLog(@"String1:%@",String1); 254 //删除原有字符,替换新字符串 255 /*-------------判断字符串内是否还包含别的字符串(前缀,后缀)-------------*/ 256 //01:检查字符串是否以另一个字符串开头- (BOOL) hasPrefix: (NSString *) aString; 257 NSString *String1 = @"NSStringInformation.txt"; 258 [String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO"); 259 /02:-------------检查方法调用字符串是否以此方法结尾-------------*/ 260 [String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO"); 261 262 //03:查找字符串某处是否包含其它字符串 - (NSRange) rangeOfString: (NSString *) aString,这一点前面在串中搜索子串用到过; 263 /******************************************************************************************* 264 NSArray 265 *******************************************************************************************/ 266 267 /*---------------------------创建数组------------------------------*/ 268 //NSArray *array = [[NSArray alloc] initWithObjects: 269 @"One",@"Two",@"Three",@"Four",nil]; 270 271 272 self.dataArray = array; 273 [array release]; 274 275 276 //- (unsigned) Count;数组所包含对象个数; 277 NSLog(@"self.dataArray cound:%d",[self.dataArray count]); 278 279 280 //- (id) objectAtIndex: (unsigned int) index;获取指定索引处的对象; 281 NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]); 282 283 284 /*--------------------------从一个数组拷贝数据到另一数组(可变数级)----------------------------*/ 285 286 287 //直接复制: 288 //创建两个空数组 289 NSArray *array1 = [[NSArray alloc] init]; 290 NSMutableArray *MutableArray = [[NSMutableArray alloc] init]; 291 //创建数组并赋值 292 NSArray *array = [NSArray arrayWithObjects: 293 @"a",@"b",@"c",nil]; 294 NSLog(@"array:%@",array); 295 //把array数组赋给空数组 296 MutableArray = [NSMutableArray arrayWithArray:array]; 297 NSLog(@"MutableArray:%@",MutableArray); 298 299 300 array1 = [NSArray arrayWithArray:array]; 301 NSLog(@"array1:%@",array1); 302 //Copy 303 id obj; 304 NSMutableArray *newArray = [[NSMutableArray alloc] init]; 305 NSArray *oldArray = [NSArray arrayWithObjects: 306 @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil]; 307 308 309 NSLog(@"oldArray:%@",oldArray); 310 for(int i = 0; i < [oldArray count]; i++) 311 { 312 obj = [[oldArray objectAtIndex:i] copy]; 313 [newArray addObject: obj]; 314 } 315 // 316 NSLog(@"newArray:%@", newArray); 317 [newArray release]; 318 319 320 //快速枚举 321 322 323 //NSMutableArray *newArray = [[NSMutableArray alloc] init]; 324 NSArray *oldArray = [NSArray arrayWithObjects: 325 @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil]; 326 NSLog(@"oldArray:%@",oldArray); 327 328 329 for(id obj in oldArray) 330 { 331 [newArray addObject: obj]; 332 } 333 // 334 NSLog(@"newArray:%@", newArray); 335 [newArray release]; 336 337 338 //Deep copy 339 340 341 //NSMutableArray *newArray = [[NSMutableArray alloc] init]; 342 NSArray *oldArray = [NSArray arrayWithObjects: 343 @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil]; 344 NSLog(@"oldArray:%@",oldArray); 345 newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers); 346 NSLog(@"newArray:%@", newArray); 347 [newArray release]; 348 349 350 //Copy and sort 351 352 353 //NSMutableArray *newArray = [[NSMutableArray alloc] init]; 354 NSArray *oldArray = [NSArray arrayWithObjects: 355 @"b",@"a",@"e",@"d",@"c",@"f",@"h",@"g",nil]; 356 NSLog(@"oldArray:%@",oldArray); 357 NSEnumerator *enumerator; 358 enumerator = [oldArray objectEnumerator]; 359 id obj; 360 while(obj = [enumerator nextObject]) 361 { 362 [newArray addObject: obj]; 363 } 364 [newArray sortUsingSelector:@selector(compare:)]; 365 NSLog(@"newArray:%@", newArray); 366 [newArray release]; 367 368 369 /*---------------------------切分数组------------------------------*/ 370 371 372 //从字符串分割到数组- componentsSeparatedByString: 373 NSString *string = [[NSString alloc] initWithString:@"One,Two,Three,Four"]; 374 NSLog(@"string:%@",string); 375 NSArray *array = [string componentsSeparatedByString:@","]; 376 NSLog(@"array:%@",array); 377 [string release]; 378 379 380 //从数组合并元素到字符串- componentsJoinedByString: 381 NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil]; 382 NSString *string = [array componentsJoinedByString:@","]; 383 NSLog(@"string:%@",string); 384 385 386 /******************************************************************************************* 387 NSMutableArray 可变数组 388 *******************************************************************************************/ 389 /*---------------给数组分配容量----------------*/ 390 //NSArray *array; 391 array = [NSMutableArray arrayWithCapacity:20]; 392 393 394 /*--------------在数组末尾添加对象----------------*/ 395 //- (void) addObject: (id) anObject; 396 //NSMutableArray *array = [NSMutableArray arrayWithObjects: 397 @"One",@"Two",@"Three",nil]; 398 [array addObject:@"Four"]; 399 NSLog(@"array:%@",array); 400 401 402 /*--------------删除数组中指定索引处对象----------------*/ 403 //-(void) removeObjectAtIndex: (unsigned) index; 404 //NSMutableArray *array = [NSMutableArray arrayWithObjects: 405 @"One",@"Two",@"Three",nil]; 406 [array removeObjectAtIndex:1]; 407 NSLog(@"array:%@",array); 408 //被删除对象后面的数组元素被前移来填补空缺 409 410 411 412 /*-------------数组枚举---------------*/ 413 //遍历索引 414 int i; 415 for (i=0; i<[array1 count]; i++) 416 { 417 NSLog(@"index %d has %@",i,[array1 objectAtIndex:i]); 418 } 419 //功能最强大 420 421 422 423 //- (NSEnumerator *)objectEnumerator;从前向后 424 //NSMutableArray *array = [NSMutableArray arrayWithObjects: 425 @"One",@"Two",@"Three",nil]; 426 NSEnumerator *enumerator; 427 enumerator = [array objectEnumerator]; 428 //创建一个泛型对象 429 id thingie; 430 //访问枚举数组的下一个对象,为nil时停止 431 while (thingie = [enumerator nextObject]) 432 433 434 { 435 NSLog(@"thingie:%@",thingie); 436 } 437 438 439 //- (NSEnumerator *)reverseObjectEnumerator;从后向前 440 //NSMutableArray *array = [NSMutableArray arrayWithObjects: 441 @"One",@"Two",@"Three",nil]; 442 NSEnumerator *enumerator; 443 enumerator = [array reverseObjectEnumerator]; 444 445 446 id object; 447 while (object = [enumerator nextObject]) { 448 NSLog(@"object:%@",object); 449 } 450 //对可变数组进行遍历操作,对可变数据进行添加删除后,enumerator遍历的是之前创建的数组,不会时时更新; 451 452 453 //快速枚举 454 //NSMutableArray *array = [NSMutableArray arrayWithObjects: 455 @"One",@"Two",@"Three",nil]; 456 for(NSString *string in array) 457 { 458 NSLog(@"string:%@",string); 459 } 460 NSmutableArray 常用方法总结array 创建一个空数组 +(id)arrayWithCapacity:size 创建一个数组,指定容量为size -(id)initWithCapacity:size 初始化一个新分配的数组,指定容量为size -(void)addObject:obj 将对象obj添加到数组末尾 -(void)insertObject:obj atIndex:i 将对象 obj 插入到索引为 i 的位置 -(void)replaceObject:obj atIndex:i 将数组中索引为 i 处的元素用obj 置换 -(void)removeObject:obj 从数组中删除所有是 obj 的对象 -(void)removeObjectAtIndex:i 从数组中删除索引为 i 的对像 -(void)sortUsingSelector:(SEL)selector 用 selector 只是的比较方法将数组排序 461 /******************************************************************************************* 462 NSDictionary 463 *******************************************************************************************/ 464 465 466 /*------------------------------------创建字典------------------------------------*/ 467 //- (id) initWithObjectsAndKeys; 468 469 470 NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil]; 471 NSString *string = [dictionary objectForKey:@"One"]; 472 NSLog(@"string:%@",string); 473 NSLog(@"dictionary:%@",dictionary); 474 [dictionary release]; 475 476 477 // 478 NSDictionary * tires; 479 480 tires =[NSDictionary dictionaryWithObjectsAndKeys: 481 t1,@"1" 482 ,t2,@"2" 483 ,t3,@"3" 484 ,t4,@"4" 485 ,nil]; 486 NSLog(@"%@",tires); 487 488 489 490 491 /******************************************************************************************* 492 NSMutableDictionary 493 *******************************************************************************************/ 494 495 496 /*------------------------------------创建可变字典------------------------------------*/ 497 //创建 498 NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; 499 //添加字典 500 [dictionary setObject:@"One" forKey:@"1"]; 501 [dictionary setObject:@"Two" forKey:@"2"]; 502 [dictionary setObject:@"Three" forKey:@"3"]; 503 [dictionary setObject:@"Four" forKey:@"4"]; 504 NSLog(@"dictionary:%@",dictionary); 505 //若已经存在该关键字,则会用新值替换原有值 506 507 508 509 //删除指定的字典 510 [dictionary removeObjectForKey:@"3"]; 511 NSLog(@"dictionary:%@",dictionary); 512 513 514 /******************************************************************************************* 515 NSValue(对任何对象进行包装) 516 *******************************************************************************************/ 517 518 519 /*--------------------------------将NSRect放入NSArray中------------------------------------*/ 520 //将NSRect放入NSArray中 521 NSMutableArray *array = [[NSMutableArray alloc] init]; 522 NSValue *value; 523 CGRect rect = CGRectMake(0, 0, 320, 480); 524 value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)]; 525 [array addObject:value]; 526 NSLog(@"array:%@",array); 527 528 529 //从Array中提取 530 value = [array objectAtIndex:0]; 531 [value getValue:&rect]; 532 NSLog(@"value:%@",value); 533 534 535 /******************************************************************************************* 536 从目录搜索扩展名为jpg的文件 537 *******************************************************************************************/ 538 539 540 //NSFileManager *fileManager = [NSFileManager defaultManager]; 541 NSString *home; 542 home = @"../Users/"; 543 544 545 NSDirectoryEnumerator *direnum; 546 direnum = [fileManager enumeratorAtPath: home]; 547 548 NSMutableArray *files = [[NSMutableArray alloc] init]; 549 550 551 //枚举 552 NSString *filename; 553 while (filename = [direnum nextObject]) { 554 if([[filename pathExtension] hasSuffix:@"jpg"]){ 555 [files addObject:filename]; 556 } 557 } 558 559 560 //快速枚举 561 //for(NSString *filename in direnum) 562 //{ 563 // if([[filename pathExtension] isEqualToString:@"jpg"]){ 564 // [files addObject:filename]; 565 // } 566 //} 567 NSLog(@"files:%@",files); 568 569 570 //枚举 571 NSEnumerator *filenum; 572 filenum = [files objectEnumerator]; 573 while (filename = [filenum nextObject]) { 574 NSLog(@"filename:%@",filename); 575 } 576 577 578 //快速枚举 579 //for(id object in files) 580 //{ 581 // NSLog(@"object:%@",object); 582 // 583 /******************************************************************************************* 584 NSNumber 585 *******************************************************************************************/ 586 //封装数据对象 587 [NSNumber numberWithChar:(char)value]; 588 [NSNumber numberWithChar:(char)value]; 589 590 [NSNumber numberWithChar:(char)value]; 591 592 [NSNumber numberWithChar:(char)value]; 593 594 [NSNumber numberWithChar:(char)value];