来自奶牛场

懂得太少,表现太多!才华太少,锋芒太多!很浅薄,一眼就能被看穿!沉下来,打根基,做一件能够持久的事;让别人觉得你没有那么浮躁和浮夸。不要把知道的那一点点东西全都表现出来,那不是你的优势。...

IOS开发中 字符串和数组NSString NSArray操作

最近写代码用到比较多的字符串操作的,所以找到一个objective-c中字符串操作的汇总,和大家分享!

 
1
//创建字符串对象数组
 NSArray *array = [str componentsSeparatedByString:@"@"];//就是以@为标示 输出看看啦
 
 int count=[array count];
 int i;
 for(i=0;i<count;i=i+4)
 {
  printf("%i:  %sn",i,[[array objectAtIndex:i] UTF8String]);
 }

2  可变的字符串类
 NSMutableString *song=[[NSMutableString alloc] init];
 [song appendString:@"Deaf Leppard"];
 printf("%sn",[song UTF8String]);
 
 NSRange range=[song rangeOfString:@"Deaf"];//获取字符串"Deaf"字串的范围
 [song replaceCharactersInRange:range withString:@"Def"];//替换 
 printf("%sn",[song UTF8String]);
 
 [song insertString:@"Animal by " atIndex:0];
 printf("%sn",[song UTF8String]);
 
 [song release];
 
3
字典加数组操作
 NSArray *keys=[@"one two three" componentsSeparatedByString:@" "];
 NSArray *value=[@"two bravo a" componentsSeparatedByString:@" "];
 NSDictionary *dic=[[NSDictionary alloc] initWithObjects:value forKeys:keys];
 printf("%sn",[[dic description] UTF8String]);



    //一、NSString    
   
 
    //1、创建常量字符串。
    NSString *astring = @"This is a String!";
 
 
    //2、创建空字符串,给予赋值。
 
    NSString *astring = [[NSString alloc] init];
    astring = @"This is a String!";
    [astring release];
    NSLog(@"astring:%@",astring);
 
 
 
    //3、在以上方法中,提升速度:initWithString方法
 
    NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
    NSLog(@"astring:%@",astring);
    [astring release];
 
 
 
    //4、用标准c创建字符串:initWithCString方法
 
    char *Cstring = "This is a String!";
    NSString *astring = [[NSString alloc] initWithCString:Cstring];
    NSLog(@"astring:%@",astring);
    [astring release];
 
 
 
    //5、创建格式化字符串:占位符(由一个%加一个字符组成)
 
    int i = 1;
    int j = 2;
    NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]];
    NSLog(@"astring:%@",astring);
    [astring release];
 
 
 
    //6、创建临时字符串
 
    NSString *astring;
    astring = [NSString stringWithCString:"This is a temporary string"];
    NSLog(@"astring:%@",astring);
 
 
 
 
        
 
    NSString *path = @"astring.text";
    NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
    NSLog(@"astring:%@",astring);
    [astring release];
 
 
        
 
 
    NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
    NSLog(@"astring:%@",astring);
    NSString *path = @"astring.text";    
    [astring writeToFile: path atomically: YES];
    [astring release];    
 
 
 
 
            
 
    //用C比较:strcmp函数
 
    char string1[] = "string!";
    char string2[] = "string!";
    if(strcmp(string1, string2) = = 0)
    {
        NSLog(@"1");
    }
 
 
 
    //isEqualToString方法    
    NSString *astring01 = @"This is a String!";
    NSString *astring02 = @"This is a String!";
    BOOL result = [astring01 isEqualToString:astring02];
    NSLog(@"result:%d",result);
 
 
 
 
    //compare方法(comparer返回的三种值)    
    NSString *astring01 = @"This is a String!";
    NSString *astring02 = @"This is a String!";    

阅读更多
个人分类: ios开发
上一篇UISearchBar相关
下一篇iOS setter &amp; getter
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭