TA的每日心情 | 汗 2024-10-15 10:05 |
---|
签到天数: 372 天 [LV.9]以坛为家II
|
本帖最后由 Sian 于 2013-12-20 09:22 编辑
main.m
- #import <Foundation/Foundation.h>
- int main(int argc, const char * argv[]){
- @autoreleasepool {
- char * s = "Hello C!"; //C语言字符串
- NSString * str = @"Hello NSString!"; //OC字符串
- NSString * srt1 = [[NSString alloc] initWithString:str]; //字符串传参
- NSString * str2 = [[NSString alloc] initWithString:@"Hello NSString2!"];
- NSString * str3 = [[NSString alloc] initWithUTF8String:"Hello C to OC!"];//用C的字符串创建OC的字符串对象
- NSString * str4 = [[NSString alloc] initWithFormat:@"Hello %d%c!",2,'B'];//使用格式符,拼接字符串
- NSString * str5 = [NSString stringWithUTF8String:"Hello C to OC! -Temp"];//对应临时字符串
- NSString * str6 = [NSString stringWithFormat:@"Helllo %d%c! -Temp",2,'B'];//对应临时字符串
- NSString * str7 = [NSString stringWithString:str];//对应临时字符串
- NSLog(@"%s",s);
- NSLog(@"%@",str);
- NSLog(@"%@",srt1);
- NSLog(@"%@",str2);
- NSLog(@"%@",str3);
- NSLog(@"%@",str4);
- NSLog(@"%@",str5);
- NSLog(@"%@",str6);
- NSLog(@"%@",str7);
- [str2 release];
- [str3 release];
- [str4 release];
- //*****************************************************
- NSString * str8 = @"Hello NSString8!";
- BOOL ret = [str isEqualToString:str8]; //比较两个字符串是否相等
- NSLog(@"%d",ret);
- //*****************************************************
- NSString * str9 = @"123";
- NSString * str10 = @"456";
- NSComparisonResult ret1 = [str9 compare:str10];//比较两个字符串大小
- if(ret1 == NSOrderedAscending){
- NSLog(@"str9 < str10!");
- }else if(ret1 == NSOrderedDescending){
- NSLog(@"str9 > str10!");
- }else if(ret1 == NSOrderedSame){
- NSLog(@"str9 = str10");
- } //str9 < str10!
- //*****************************************************
- NSString * str11 = @"I am a so bad man.";
- NSString * subStr = @"so";
- NSRange range = [str11 rangeOfString:subStr];
- NSLog(@"location:%lu length:%lu",range.location,range.length);//location:7 length:2
- //*****************************************************
- NSString * str12 = @"0123456789";
- NSString * str13 = [str12 substringToIndex:6]; //从str中提取从头至第6个字符串(从0开始计算则不包含第6个的开区间),生成新的字符串
- NSString * str14 = [str12 substringFromIndex:6]; //从第6个字符开始提取,直至字符串尾,返回字符串地址
- NSString * str15 = [str12 subStringWithRange:range1];
- //NSString * str16 = [str12 substringWithRange:NSMakeRange(4, 4)];
- NSLog(@"%@",str12);
- NSLog(@"%@",str13);
- NSLog(@"%@",str14);
- NSLog(@"%@",str15);
- //NSLog(@"%@",str16);
- }
- return 0;
- }
复制代码 |
|