String (nsstring) of the second data type in objective-C)

Source: Internet
Author: User

        // 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);

String (nsstring) of the second data type in objective-C)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.