字符串的赋值和字符串数组
// // main.m // stringTest // // Created by wen jing on 12-8-4. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); ////////////关于字符串的赋值////////////// //1)赋值方法常量 NSString *str = @"This is a String!"; /* Log: 2012-08-04 22:32:58.687 stringTest[441:903] Hello, World! */ //输出字符串 %@ NSLog(@"str value==%@",str); /* log: 2012-08-04 22:32:58.701 stringTest[441:903] str value==This is a String! */ //2)赋值方法 NSString *str2 = [[NSString alloc] init]; str2 = @"this is a String 2"; NSLog(@"str2 value=%@",str2); [str2 release];//释放内存 /* Log: 2012-08-04 22:37:30.295 stringTest[496:903] str1 value=this is a String 2 */ //3赋值方法(效率高) NSString *str3=[[NSString alloc] initWithString:@"this is a string 3"]; NSLog(@"str3 value=%@",str3); [str3 release]; /* Log: 2012-08-04 22:48:33.439 stringTest[568:903] str3 value=this is a string 3 */ //4)赋值方法 NSString *str4=[[NSString alloc] initWithCString:"this is a string 4"]; NSLog(@"str4 value=%@",str4); [str4 release]; /* Log: 2012-08-04 22:54:49.784 stringTest[646:903] str4 value=this is a string 4 */ //5)赋值方法 int i = 1; int j = 2; NSString *str5 = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]]; NSLog(@"str5 value=%@",str5); [str5 release]; /* log: 2012-08-04 23:06:17.362 stringTest[696:903] str5 value=1.This is 2 string! */ //6、创建临时字符串 NSString *str6; str6 = [NSString stringWithCString:"This is a temporary string"]; NSLog(@"astring:%@",str6); /* log: 2012-08-04 23:08:04.335 stringTest[716:903] astring:This is a temporary string */ // //数字================== NSString *str7 =[[NSString alloc] initWithString:@"fdsajkkfsdakfa@dskjkafsdk"]; NSArray *array = [str7 componentsSeparatedByString:@"@"];//就是以@为标示 输出看看啦 int count=[array count]; for(int j=0;j<count;j++) { printf("%i %i: %s ",count,j,[[array objectAtIndex:j] UTF8String]); } /* log: 2 0: fdsajkkfsdakfa 2 1: dskjkafsdk */ NSMutableString *song=[[NSMutableString alloc] init]; [song appendString:@"Deaf Leppard"]; printf("%s ",[song UTF8String]); /* log: Deaf Leppard */ // 字典加数组操作 NSArray *keys=[@"one two three" componentsSeparatedByString:@" "]; NSArray *value=[@"two bravo a" componentsSeparatedByString:@" "]; NSDictionary *dic=[[NSDictionary alloc] initWithObjects:value forKeys:keys]; printf("%s ",[[dic description] UTF8String]); /* log: { one = two; three = a; two = bravo; } */ } return 0; }
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。