IOS NSString to Char,char to nsstring, string compare

小结:

1) cStringUsingEncoding:string -->to char

2) initWithCString:      char --> string

3) [string length] >0 ,[string isEqualToString:@"aaa"  比较区分大小写

4) main 函数里调用的方法要放在main函数上面,先加载再执行。

void cStringConvertTest(){

//NSString 转换为char *

NSString *blankText = @"sevensoft is a mobile software outsourcing company";

char *ptr = [blankText cStringUsingEncoding:NSASCIIStringEncoding];

printf("ptr:%s\n", ptr);

//char * 转换为 NSString

char encode_buf[1024];

NSString *encrypted = [[NSString alloc] initWithCString:(const char*)encode_buf encoding:NSASCIIStringEncoding];

NSLog(@"encrypted:%@", encrypted);

}



void stringCompareTest(){

NSString *string = @""

//判断字符串是否为空:

//错误写法1

// if(string == nil){

// }

//错误写法2

// if (string == [NSNull null] || string.length == 0 ){

// }

// 正确写法

if([string length] >0){

NSLog(@"string length >0");

}

if([string isEqualToString:@"Some String"]){

NSLog(@"Equal to 'Some String'");

}

}


 

posted @ 2012-07-16 10:12  大树2  阅读(9975)  评论(1编辑  收藏  举报