字符串的赋值和字符串数组

字符串在任何语言中是很重要,所以我们应该熟练的掌握

//
//  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\n",count,j,[[array objectAtIndex:j] UTF8String]);
        }
     /*
      log:
      2 0: fdsajkkfsdakfa
      2 1: dskjkafsdk
      
      
      
      */
        
        
        NSMutableString *song=[[NSMutableString alloc] init];
        [song appendString:@"Deaf Leppard"];
        
        printf("%s\n",[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\n",[[dic description] UTF8String]);
        
        /*
         log:
         {
         one = two;
         three = a;
         two = bravo;
         }
         
         
         
         */
        
    }
    return 0;
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值