IOS NSString

	//NSString          
	//     
	NSString *szTmp = @"A string";		//    
	szTmp = nil;
	
	int n = 5;
	NSString *szMyString = [NSString stringWithFormat:@"The number is %d",n];	//The number is 5
	[szMyString stringByAppendingFormat:@"%d",22];	//        :The number is 522
													//  szMyString       ,    :The number is 5	

	//       
	NSLog(@"%d",szMyString.length);					//     :15
	NSLog(@"%c",[szMyString characterAtIndex:2]);	//    :e
	

	// c       
	printf("%s
",[szMyString UTF8String]); // __strong const char * const char *szTmp1 = [szMyString cStringUsingEncoding:NSUTF8StringEncoding]; printf("%s
",szTmp1); // __strong const char * NSLog(@"%@",[NSString stringWithCString:szTmp1 encoding:NSUTF8StringEncoding]); // nsstring

	//      
	NSError *error;
	NSString *szPath = [NSHomeDirectory()			//        
						stringByAppendingPathComponent:@"Documents/testFile.txt"];	//      
	if (![szMyString writeToFile:szPath atomically:YES	//atomically:          
						encoding:NSUTF8StringEncoding error:&error]) {			//      yes   no
		NSLog(@"Error writing to file :%@",[error localizedDescription]);		//      
		return 1;
	}
	NSLog(@"File write success");
	

	//      
	NSString *szInString = [NSString stringWithContentsOfFile:szPath			//      
							encoding:NSUTF8StringEncoding error:&error];
	if (!szInString)
	{
		//  
	}
	NSLog(@"%@",szInString);		//  
	

	//       
	NSArray *arrayWord = [szMyString componentsSeparatedByString:@" "];	//           
	NSLog(@"%@",arrayWord);
	

	//    
	NSString *szSub1 = [szMyString substringToIndex:3];		//0-2, 3 :The
	NSLog(@"%@",szSub1);
	
	NSString *szSub2 = [szMyString substringFromIndex:4];	//4- ,   4 :number is 5
	NSLog(@"%@",szSub2);
	

	//    
	NSRange range;
	range.location = 4;		// 4  
	range.length = 6;		//6   
	NSString *szSub3 = [szMyString substringWithRange:range];		//number
	NSLog(@"%@",szSub3);
	

	//     
	NSRange rangeSearch = [szMyString rangeOfString:@"is 5"];	//  
	if (rangeSearch.location != NSNotFound) {			//      NSNotFound
		//  :rangeSearch.location;//   rangeSearch.length;//  
	}
	
	NSLog(@"%@",[szMyString stringByReplacingCharactersInRange:rangeSearch		//       
													withString:@"isn't 10"]);
	
	NSString *szReplaced = [szMyString stringByReplacingOccurrencesOfString:@" " withString:@"*"];	//       
	NSLog(@"%@",szReplaced);
	

	//     
	NSLog(@"%@",[szMyString uppercaseString]);		//  
	NSLog(@"%@",[szMyString lowercaseString]);		//  
	NSLog(@"%@",[szMyString capitalizedString]);	//     
	

	//     
	NSString *sz1 = @"Hello World!";
	NSString *sz2 = @"Hello Mom!";
	if ([sz1 isEqualToString:sz2]) {/*  */}
	if ([sz1 hasPrefix:@"Hello"]) {NSLog(@"     ");}		//      
	if ([sz1 hasSuffix:@"d!"]) {NSLog(@"     ");}		//     
	

	//       
	NSString *szNumber = @"3.14";
	[szNumber intValue];
	[szNumber boolValue];
	[szNumber floatValue];
	[szNumber doubleValue];
	

	//     
	NSMutableString *szMuMyString = [NSMutableString stringWithString:@"Hello"];
	[szMuMyString appendFormat:@"World"];		//   ,    
	[szMuMyString uppercaseString];
	NSLog(@"%@",szMuMyString);
	

좋은 웹페이지 즐겨찾기