码迷,mamicode.com
首页 > 其他好文 > 详细

objective-c 中数据类型之二 字符串(NSString)

时间:2014-10-10 19:07:34      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:des   io   os   使用   ar   for   数据   sp   art   

        // 1. 声明一个NSString对象,注意对象前要加‘*’;
        NSString *string1;
         
        // 赋值方式1,初始化赋值;
        NSString *string2 = [[NSString alloc] initWithString: @"hello world!"];
         
        // 赋值方式2;
        string1 = @"hello world!";
        string2 = [NSString stringWithString: @"hello world!"];
         
        // 赋值方式3,带格式赋值,方便实用;
        string1 = [NSString stringWithFormat: @"Hi, I'm %@,hello to my world!", @"NSString"];
        NSLog(@"%@", string1);
         
        // 2. 控制台输入一个字符串给string;
        char *str = NULL;   // 声明char * 字符串;
        str = alloca(20);   // 给str分配内存;
        scanf("%s", str);   // 控制台输入字符串给str;
        // 将str赋给NSString对象string;
        string1 = [NSString stringWithUTF8String: str];
        // 输出string;
        NSLog(@"string: %@", string1);
         
        // 3. 字符串长度length的使用;
        if ([string1 length] > 5) {
            NSLog(@"字符串长度大于5.");
        } else {
            NSLog(@"字符串长度不大于5.");
        }
         
        // 4.字符串比较是否相等,isEqualToString返回一个布尔型数据YES\NO;
        string1 = @"hello world";
        string2 = @"hello world";
        if ([string1 isEqualToString:string2]) {
            NSLog(@"%@ 等于 %@", string1, string2);
        } else {
            NSLog(@"%@ 不等于 %@", string1, string2);
        }
         
        string1 = @"hello world";
        string2 = @"hello worla";
        if ([string1 isEqualToString:string2]) {
            NSLog(@"%@ 等于 %@", string1, string2);
        } else {
            NSLog(@"%@ 不等于 %@", string1, string2);
        }
         
        // 注:这里 isEqualToString: 不同于 ==,前者比较是否等价,后者比较指针数值,是否是同一对象;
         
        // 5. 字符串比较大小,
        // 5.1 区分大小写的比较compare:返回NSComparisonResult(enum)型数据,
        //     小于返回NSOrderedAscending(值为-1),等于返回NSOrderedSame(值为0),大于返回NSOrderedDescending(值为1);
          
        string1 = @"hello worlD"; // hello worlD 小于 hello world;
        string2 = @"hello world";
         
        string2 = @"hello world"; // hello world 等于 hello world;
        string2 = @"hello world";
         
        string2 = @"hello world"; // hello worle 大于 hello world;
        string2 = @"hello world";
        if ([string1 compare:string2] == NSOrderedSame) {
            NSLog(@"%@ 等于 %@", string1, string2);
        } else if ([string1 compare:string2] == NSOrderedAscending){
            NSLog(@"%@ 小于 %@", string1, string2);
        } else if ([string1 compare:string2] == NSOrderedDescending){
            NSLog(@"%@ 大于 %@", string1, string2);
        }
         
        // 5.2 高级比较compare:options:返回NSComparisonResult(enum)型数据,小于返回-1,等于返回0,大于返回1;
        // options: NSCaseInsensitiveSearch不区分大小写;NSLiteralSearch区分大小写;NSNumericSearch比较字符串的字符个数;
        string1 = @"hello worlDa";
        string2 = @"hello world";
        // 不区分大小写,比较字符串的字符个数;
        if ([string1 compare:string2 options:NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame) {
            NSLog(@"%@ 等于 %@", string1, string2);
        } else if ([string1 compare:string2 options:NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedAscending){
            NSLog(@"%@ 小于 %@", string1, string2);
        } else if ([string1 compare:string2 options:NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedDescending){
            NSLog(@"%@ 大于 %@", string1, string2);
        }
         
        // 6. 字符串的连接;
        NSString* string; // 结果字符串,将string1和string2连接起来
        //方法1.
        string1 = @"hello ";
        string2 = @"world";
        string = [[NSString alloc]initWithFormat:@"%@%@", string1, string2]; // 字符之间不加逗号;
        NSLog(@"string1: %@", string1); // 输出string1;   
        NSLog(@"string2: %@", string2); // 输出string2;
        NSLog(@"string: %@", string);   // 输出string;
         
        //方法2.
        // 将string1与string2合并后赋给string;
        string1 = @"game ";
        string2 = @"start";
        NSLog(@"将string1与string2合并后赋给string前:");
        NSLog(@"string1: %@", string1); // 输出string1;
        NSLog(@"string2: %@", string2); // 输出string2;
        NSLog(@"string: %@", string); // 输出string;
        string = [string1 stringByAppendingString:string2];
        NSLog(@"将string1与string2合并后赋给string后:");
        NSLog(@"string1: %@", string1); // 输出string1;
        NSLog(@"string2: %@", string2); // 输出string2;
        NSLog(@"string: %@", string); // 输出string;
         
        //方法3.
        // 将string1, string2加到string的后面;
        string = @"this ";
        string1 = @"game ";
        string2 = @"over";
        NSLog(@"将string1, string2加到string的后面前:");
        NSLog(@"string: %@", string); // 输出string;
        NSLog(@"string1: %@", string1); // 输出string1;
        NSLog(@"string2: %@", string2); // 输出string2;
         
        string = [string stringByAppendingFormat:@"%@%@", string1, string2]; // 字符之间不加逗号;
        NSLog(@"将string1, string2加到string的后面后:");
        NSLog(@"string: %@", string); // 输出string;
         
        // 7. 字符串的查找,是否以一个字符串开头hasPrefix:,是否以一个字符串结尾hasSuffix:,是否包含字符串rangeOfString:;
        NSRange range;
        string = @"jingchagushi_chapter.rmvb";
        string1 = @"jing";
        string2 = @"rmvb";
        if ([string hasPrefix:string1]) {
            NSLog(@"%@ 的开头是%@",string, string1);
        }
        if ([string hasSuffix:string2]) {
            NSLog(@"%@ 的结尾是%@",string, string2);
        }
        range = [string rangeOfString:@"ch"];
        NSLog(@"位置%lu,长度%lu", range.location, range.length);

objective-c 中数据类型之二 字符串(NSString)

标签:des   io   os   使用   ar   for   数据   sp   art   

原文地址:http://blog.csdn.net/yanglishuan/article/details/39964749

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!