objective-c 中資料類型之二 字串(NSString)

來源:互聯網
上載者:User

標籤: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)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.