Objective-c 基本数据结构之NSString

#import <Foundation/Foundation.h>


int main(int argc, const char * argv[])

{


    @autoreleasepool {

        /**************************字符串声明 start**************************/

//        // insert code here...

//        NSLog(@"Hello, World!");

//        //创建字符串,4.2版本以前需要释放内存,4.2版本以后不需要

//        NSString * str = @"hello word,中国";

//        

//        NSString * str2 = [[NSString alloc] initWithString:str];

//        

//        //用c的字符串创建oc的字符串对象

//        NSString * str3 = [[NSString alloc] initWithUTF8String:"hello world"];

//        

//        //使用格式符,拼接成一个字符串

//        NSString * str4 = [[NSString alloc] initWithFormat:@"hello %d %c",5,'A'];

//        

//        

//        

//        //三个临时字符串不用释放内存,会自动释放内存

//        NSString * str5 = [NSString stringWithUTF8String:"hello word"];

//        

//        NSString * str6 = [NSString stringWithFormat:@"%d %@ %c", 1, @"hello", 'R'];

//        

//        NSString * str7 = [NSString stringWithString:str];

//        

//        

//        

//        NSLog(@"str is %@", str);

//        NSLog(@"str2 is %@", str2);

//        NSLog(@"str3 is %@", str3);

//        NSLog(@"str4 is %@", str4);

//        NSLog(@"str5 is %@", str5);

//        NSLog(@"str6 is %@", str6);

//        NSLog(@"str7 is %@", str7);

        

        

        /**************************字符串声明 end**************************/

        

        

        /**************************字符串用法 start**************************/

        

        //一、字符串合并只限于可变字符传

        

        //创建了一个空间大小为50的的可变字符串

        NSMutableString * str2 = [NSMutableStringstringWithCapacity:50];

        NSString * str = @"myXcodeStudy";

        

        //追加

        [str2 appendString:str];

        [str2 appendFormat:@"%d",5];

        [str2 appendFormat:@"%c", 'D'];


        NSLog(@"str2 is %@",str2);

        

        //插入

        [str2 insertString:@"InsertOfString" atIndex:4];

        NSLog(@"在str2字符串的第4个位置插入了'InsertOfString'%@ ", str2);

        [str2 insertString:[NSStringstringWithFormat:@"%d%c ", -520,'A'] atIndex:6];

        NSLog(@"在str2字符串的第6个位置插入了'-520与A'%@", str2);

        

        //二、字符串删减需要辅助结构体

        //创建了一个空间大小为5的的可变字符串

        NSMutableString * delStr = [NSMutableStringstringWithCapacity:5];

        

        [delStr appendString:@"I am man in family,hello"];

        //指定范围删除字符串,找到字符串的起始位置,包含的字符串个数

        NSRange range = [delStr rangeOfString:@"am"];

        

        //删除字符串 2种写法,结果一样,建议第二种

        //[delStr deleteCharactersInRange:range];

        [delStr deleteCharactersInRange:NSMakeRange(2, 2)];

        NSLog(@"%@, %lu, %lu ,%lu",delStr, delStr.length,range.length, range.location);

        

        //指定范围替换字符串

        [delStr replaceCharactersInRange:NSMakeRange(3, 3) withString:@"REPLACE"];

        

        NSLog(@"指定范围内替换字符串%@ ", delStr);

        

        

        //截取字符串

        NSString *cutStr = @"0123456789";

        //从cutStr中提取从头至第6个字符串(不包括第6个),生成新字符串,返回字符串地址,开区间

        NSString *cutStr1 = [cutStr substringToIndex:6];

        //从cutStr提取从第6个字符(包括),到最后一个字符,生成新字符串,返回字符串地址,闭区间

        NSString *cutStr2 = [cutStr substringFromIndex:6];

        //根据指定范围提取字符串

        NSString *cutStr3 = [cutStr substringWithRange:NSMakeRange(3, 4)];

        

        NSLog(@"从cutStr中提取从头至第6个字符串,返回结果:%@", cutStr1);

        NSLog(@"从cutStr提取从第6个字符到结尾,返回结果:%@", cutStr2);

        NSLog(@"根据指定范围提取字符串,返回结果:%@", cutStr3);


        

        

        

        //三、字符产比较

        

        //字符串是否相等

        NSString * compareStr = @"helloword";

        NSString * compareStr1 = @"helloword";

        

        //比较两个字符串是否相等,返回值YES 或者 NO

        if ([compareStr isEqualToString:compareStr1]) {

            NSLog(@"两个字符串相等");

        } else {

            NSLog(@"两个字符串不相等");

        }

        

        

        //字符串大小比较 compare 三种返回值 可以加option参数

        NSString *compStr = @"helloworld";

        NSString *compStr1 = @"worldhello";

        

        NSComparisonResult res = [compStr compare:compStr1];

        if (res == NSOrderedSame) {

            NSLog(@"compStr = compStr2");

        } else if (res == NSOrderedAscending) {

            NSLog(@"compStr < compStr2");

        } else {

            NSLog(@"compStr1 > compStr2");

        }

        

    }

    return 0;

}


评论
热度 ( 1 )

© lightingbolt | Powered by LOFTER