真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Objective-C之字符串對(duì)象-創(chuàng)新互聯(lián)

Foundation框架支持一個(gè)名為NSString的類(lèi),用于處理字符串對(duì)象。
注意 : 要使用OC語(yǔ)言創(chuàng)建一個(gè)字符串對(duì)象,需要在字符串開(kāi)頭放置一個(gè)@字符:

措勤網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),措勤網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為措勤上千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的措勤做網(wǎng)站的公司定做!
@"Programming is fun"

一 : NSLog
%@用來(lái)顯示NSString。

#import 

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str = @"Programming is fun";
        NSLog(@"%@",str);
    }
    return 0;
}

結(jié)果:
Objective-C之字符串對(duì)象

當(dāng)然值得注意的是 , %@可以顯示其他的對(duì)象:

#import 

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str = @"Programming is fun";
        NSLog(@"%@",str);

        NSNumber *initNum = [NSNumber numberWithInteger:100];
        NSLog(@"%@",initNum);
    }
    return 0;
}

結(jié)果:
Objective-C之字符串對(duì)象

二 : 基本字符串操作①

#import 

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str1 = @"This is string A --";
        NSString *str2 = @"This is string B";

        NSString *res;
        NSComparisonResult compareResult;

        //計(jì)算字符串中的字符
        NSLog(@"Length of str1 : %lu" , [str1 length]);

        //將一個(gè)字符串復(fù)制到另一個(gè)字符串(全部覆蓋(擦掉覆蓋))
        res = [NSString stringWithString:str1];
        NSLog(@"copy : %@", res);
        res = [NSString stringWithString:str2];
        NSLog(@"copy : %@", res);

        //將一個(gè)字符串復(fù)制到另一個(gè)字符串的末尾(在str1后面加str2)
        str2 = [str1 stringByAppendingString:str2];
        NSLog(@"Concatentation : %@" , str2);

        //驗(yàn)證2個(gè)字符串是否相等
        if( [str1 isEqualToString: res] == YES){
            NSLog(@" str1 == res ");
        }else{
            res = [NSString stringWithString:str1];
            if([str1 isEqualToString:res] == YES){
                NSLog(@"str1 == res 2");
            }
        }

        //驗(yàn)證一個(gè)字符串死否小于,等于或大于另一個(gè)字符串

        compareResult = [str1 compare:str2];
        if( compareResult == NSOrderedAscending ){
            NSLog(@"str1 < str2");
        }else if( compareResult == NSOrderedSame ){
            NSLog(@"str1 == str2");
        }else{
            //NSOrderedDescending
            NSLog(@"str1 > str2");
        }

        //將字符串轉(zhuǎn)為大寫(xiě)(注意 : str1的大小寫(xiě)不變)
        res = [str1 uppercaseString];
        NSLog(@"str1 : %@ , res : %@" , str1 , res);

        //將字符串轉(zhuǎn)換成小寫(xiě)
        res = [str1 lowercaseString];
        NSLog(@"str1 : %@ , res : %@" , str1 , res);
    }
    return 0;
}

結(jié)果:

Objective-C之字符串對(duì)象

二 : 基本字符串操作②

#import 

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str1 = @"This is string A --";
        NSString *str2 = @"This is string B";

        NSString *res;
        NSRange subRange;

        //從字符串中提取前3個(gè)字符
        res = [str1 substringToIndex:3];
        NSLog(@"First 3 chars of str1 : %@" , res);

        //提取從索引5開(kāi)始直到結(jié)尾的子字符串
        res = [str1 substringFromIndex:5];
        NSLog(@"Chars from index 5 of str1 : %@", res);

        //提取從索引5開(kāi)始到索引13的子字符串(6個(gè)字符)
        res = [[str1 substringFromIndex:8] substringToIndex:6];
        NSLog(@"Chars from index 8 through 13 : %@" , res);

        //更簡(jiǎn)單的方法  提取從索引5開(kāi)始到索引13的子字符串(6個(gè)字符)
        res = [str1 substringWithRange:NSMakeRange(8,6)];
        NSLog(@"!! Chars from index 8 through 13 : %@" , res);

        //查找字符串
        subRange = [str1 rangeOfString:@"string"];
        NSLog(@"String is at index %lu ,length is %lu" , subRange.location , subRange.length);

        subRange = [str1 rangeOfString:@"string B"];
        //沒(méi)有找到
        if( subRange.location == NSNotFound ){
            NSLog(@"String not found");
        }
    }
    return 0;
}

結(jié)果:
Objective-C之字符串對(duì)象

三 :可變字符串
NSMutableString 類(lèi)可以用來(lái)創(chuàng)建可以更改字符的字符串對(duì)象,它繼承自NSString

#import 

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *str1 = @"This is string A";
        NSString *search , *replace;
        NSMutableString *mstr;
        NSRange substr;

        //從不可變字符串創(chuàng)建可變字符串
        mstr = [NSMutableString stringWithString: str1];
        NSLog(@"%@",mstr);

        //插入字符
        [mstr insertString:@" Aonaufly" atIndex:7];
        NSLog(@"%@",mstr);

        //插入末尾進(jìn)行有效拼接
        [mstr insertString:@" and string B" atIndex:[mstr length]];
        NSLog(@"%@",mstr);
        //same
        [mstr appendString:@" and string C"];
        NSLog(@"%@",mstr);

        //根據(jù)范圍刪除子字符串
        [mstr deleteCharactersInRange: NSMakeRange(16,13)];
        NSLog(@"%@",mstr);

        //查找然后將其刪除
        substr = [mstr rangeOfString:@"string B and "];
        if( substr.location != NSNotFound ){
            [mstr deleteCharactersInRange:substr];
            NSLog(@"%@",mstr);
        }

        //直接設(shè)置為可變的字符串
        [mstr setString:@"This is string A"];
        NSLog(@"%@",mstr);

        //替換一些字符串
        [mstr replaceCharactersInRange:NSMakeRange(8,8) withString:@"a mutable string"];
        NSLog(@"%@",mstr);

        //查找和替換
        search = @"This is";
        replace = @"An example of";
        substr = [mstr rangeOfString:search];
        if(substr.location != NSNotFound){
            [mstr replaceCharactersInRange: substr withString:replace];
            NSLog(@"%@",mstr);
        }

        //查找和替換所有的匹配項(xiàng)
        search = @"a";
        replace = @"X";
        substr = [mstr rangeOfString:search];
       while (substr.location != NSNotFound) {
            [mstr replaceCharactersInRange:substr withString:replace];
            substr = [mstr rangeOfString:search];
        }
        NSLog(@"%@",mstr);
    }
    return 0;
}

結(jié)果:
Objective-C之字符串對(duì)象

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。


分享題目:Objective-C之字符串對(duì)象-創(chuàng)新互聯(lián)
轉(zhuǎn)載來(lái)源:http://weahome.cn/article/dsceih.html

其他資訊

在線(xiàn)咨詢(xún)

微信咨詢(xún)

電話(huà)咨詢(xún)

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部