NSString簡(jiǎn)單細(xì)說(shuō)(八)—— 識(shí)別和比較字符串

版本記錄

版本號(hào) 時(shí)間
V1.0 2017.05.07

前言

前面我簡(jiǎn)單的寫(xiě)了些NSString的初始化舔糖,寫(xiě)了幾篇俱济,都不難,但是可以對(duì)新手有一定的小幫助域携,對(duì)于大神級(jí)人物可以略過(guò)這幾篇棺弊,NSString本來(lái)就沒(méi)有難的晶密,都是細(xì)枝末節(jié),忘記了查一下就會(huì)了模她,沒(méi)有技術(shù)難點(diǎn)稻艰,下面我們繼續(xù)~~~
1. NSString簡(jiǎn)單細(xì)說(shuō)(一)—— NSString整體架構(gòu)
2. NSString簡(jiǎn)單細(xì)說(shuō)(二)—— NSString的初始化
3. NSString簡(jiǎn)單細(xì)說(shuō)(三)—— NSString初始化
4. NSString簡(jiǎn)單細(xì)說(shuō)(四)—— 從URL初始化
5. NSString簡(jiǎn)單細(xì)說(shuō)(五)—— 向文件或者URL寫(xiě)入
6. NSString簡(jiǎn)單細(xì)說(shuō)(六)—— 字符串的長(zhǎng)度
7. NSString簡(jiǎn)單細(xì)說(shuō)(七)—— 與C字符串的轉(zhuǎn)化

詳述

識(shí)別和比較字符串

一、- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;

這個(gè)用于字符串的比較侈净,比較結(jié)果是個(gè)枚舉NSComparisonResult尊勿。如下:

typedef enum NSComparisonResult : NSInteger {
    NSOrderedAscending = -1L,
    NSOrderedSame,
    NSOrderedDescending
} NSComparisonResult;
```

這里

```
NSOrderedAscending  //左邊的小于右邊的
        The left operand is smaller than the right operand.
NSOrderedSame  //左邊的字符串等于右邊的
        The two operands are equal.
NSOrderedDescending  //左邊的字符串大于右邊的
        The left operand is greater than the right operand.
```

下面我們直接看代碼。

```

    /**
     *1.- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
     *
     *  @param string:The string with which to compare the receiver.
     *
     *  @return :Returns an NSComparisonResult value that indicates the lexical ordering. NSOrderedAscending the receiver precedes aString in lexical ordering, NSOrderedSame the receiver and aString are equivalent in lexical value, and NSOrderedDescending if the receiver follows aString.
     *
     */
    
    NSString *ocStr1 = @"AABBCC";
    NSString *ocStr2 = @"aABBCC";
    NSString *ocStr3 = @"AABCCC";
    
    NSComparisonResult result1 = [ocStr1 caseInsensitiveCompare:ocStr2];
    NSLog(@"result1--%ld",result1);

    NSComparisonResult result2 = [ocStr1 caseInsensitiveCompare:ocStr3];
    NSLog(@"result2--%ld",result2);

```

看結(jié)果畜侦。

```
2017-05-07 15:06:52.485 NSString你會(huì)用嗎元扔?[2596:103484] result1--0
2017-05-07 15:06:52.485 NSString你會(huì)用嗎?[2596:103484] result2---1

```

**結(jié)論**:0代表左右相等旋膳,也就是說(shuō)這個(gè)方法比較不區(qū)分大小寫(xiě)澎语,-1代表上升,左邊小于右邊验懊,1代表下降擅羞,左邊大于右邊。因?yàn)閛cStr1的第四位是B义图,ocStr3的第四位是C减俏,則前者小于后者,返回的是-1碱工。需要說(shuō)明的是這個(gè)方法和 compare:options:方法option 參數(shù)取值NSCaseInsensitiveSearch時(shí)是一樣的娃承。當(dāng)處理的字符串是要呈現(xiàn)給用戶(hù)時(shí),需要使用的是localizedCaseInsensitiveCompare:方法怕篷。為什么要用這個(gè)方法历筝?因?yàn)橛行┱Z(yǔ)言并不是基于英文字母的。例如: 對(duì)于漢語(yǔ)字符廊谓,就存在[A-Z]字母表和漢字發(fā)音之間的對(duì)應(yīng)關(guān)系, 而且對(duì)于app用戶(hù)而言, 漢語(yǔ)字符順序'基本上'是基于發(fā)音的漫谷。'基本上'意味著并不是100%遵從這條規(guī)則。有些生僻漢字不是基于發(fā)音 (這些漢字如此生僻, 以至于你可以認(rèn)為發(fā)音就是正確的排序規(guī)則)蹂析。

----------

### 二舔示、- (NSComparisonResult)localizedCaseInsensitiveCompare:(NSString *)string;

看代碼。

```

    /**
     *2. - (NSComparisonResult)localizedCaseInsensitiveCompare:(NSString *)string;
     *
     *  @param string:This value must not be nil. If this value is nil, the behavior is undefined and may change in future versions of macOS.     
     *
     *  @return :Returns an NSComparisonResult value that indicates the lexical ordering. NSOrderedAscending the receiver precedes aString in lexical ordering, NSOrderedSame the receiver and aString are equivalent in lexical value, and NSOrderedDescending if the receiver follows aString.
     *
     */
    
    NSString *ocStr1 = @"good morning";
    NSString *ocStr2 = @"早上好";
    
    NSComparisonResult result1 = [ocStr1 localizedCaseInsensitiveCompare:ocStr2];
    NSLog(@"result1--%ld",result1);


```

看結(jié)果电抚。

```
2017-05-07 15:50:06.849 NSString你會(huì)用嗎惕稻?[3211:138318] result1---1

```

**結(jié)論**:這個(gè)要根據(jù)locale來(lái)確定,是不區(qū)分大小寫(xiě)的比較蝙叛。

-----------


### 三俺祠、- (NSComparisonResult)compare:(NSString *)string;

看代碼。

```
    /**
     *3. - (NSComparisonResult)compare:(NSString *)string;
     *
     *  @param string:This value must not be nil. If this value is nil, the behavior is undefined and may change in future versions of macOS.
     *
     *  @return :Returns an NSComparisonResult value that indicates the lexical ordering. NSOrderedAscending the receiver precedes aString in lexical ordering, NSOrderedSame the receiver and aString are equivalent in lexical value, and NSOrderedDescending if the receiver follows aString.
     *
     */
    
        NSString *ocStr1 = @"AABBCC";
        NSString *ocStr2 = @"aABBCC";
        NSString *ocStr3 = @"AABCCC";
    
        NSComparisonResult result1 = [ocStr1 compare:ocStr2];
        NSLog(@"result1--%ld",result1);
    
        NSComparisonResult result2 = [ocStr1 compare:ocStr3];
        NSLog(@"result2--%ld",result2);

```

看結(jié)果借帘。

```
2017-05-07 16:02:02.135 NSString你會(huì)用嗎蜘渣?[3373:145514] result1---1
2017-05-07 16:02:02.136 NSString你會(huì)用嗎?[3373:145514] result2---1

```

**結(jié)論**:結(jié)果是-1肺然,都是上升的蔫缸,左邊小于右邊,也就是說(shuō)它們的比較是區(qū)分大小寫(xiě)的际起。同樣拾碌,如果處理的文本是要呈現(xiàn)給用戶(hù)時(shí),這時(shí)候使用的應(yīng)該是localizedStandardCompare: 方法街望。

-------------

### 四校翔、- (NSComparisonResult)localizedCompare:(NSString *)string;

```

    /**
     *4. - (NSComparisonResult)localizedCompare:(NSString *)string;
     *
     *  @param string:This value must not be nil. If this value is nil, the behavior is undefined and may change in future versions of macOS.
     *
     *  @return :Returns an NSComparisonResult value that indicates the lexical ordering. NSOrderedAscending the receiver precedes aString in lexical ordering, NSOrderedSame the receiver and aString are equivalent in lexical value, and NSOrderedDescending if the receiver follows aString.
     *
     */
    
        NSString *ocStr1 = @"早上好";
        NSString *ocStr2 = @"早上好";
    
        NSComparisonResult result1 = [ocStr1 localizedCompare:ocStr2];
        NSLog(@"result1--%ld",result1);


```

看結(jié)果。

```

2017-05-07 16:15:13.405 NSString你會(huì)用嗎灾前?[3750:157885] result1--0

```

**結(jié)論**:本地化字符串的比較防症,同樣,如果處理的文本是要呈現(xiàn)給用戶(hù)時(shí)哎甲,這時(shí)候使用的應(yīng)該是localizedStandardCompare: 方法蔫敲。

----------

### 五、- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;

這里重要的就是這個(gè)option參數(shù)烧给,如下:

```

typedef NS_OPTIONS(NSUInteger, NSStringCompareOptions) {
    //不區(qū)分大小寫(xiě)的比較
    NSCaseInsensitiveSearch = 1,

    //區(qū)分大小寫(xiě)的比較
    NSLiteralSearch = 2,        /* Exact character-by-character equivalence */

    //從字符串末尾開(kāi)始搜索
    NSBackwardsSearch = 4,      /* Search from end of source string */

    //搜索限制范圍的字符串
    NSAnchoredSearch = 8,       /* Search is limited to start (or end, if NSBackwardsSearch) of source string */

    //按照字符串里面的數(shù)字為依據(jù)進(jìn)行比較
    NSNumericSearch = 64,       /* Added in 10.2; Numbers within strings are compared using numeric value, that is, Foo2.txt < Foo7.txt < Foo25.txt; only applies to compare methods, not find */

    //忽略"-"符號(hào)的比較
    NSDiacriticInsensitiveSearch NS_ENUM_AVAILABLE(10_5, 2_0) = 128, /* If specified, ignores diacritics (o-umlaut == o) */

    //忽略字符串的長(zhǎng)度算出比較結(jié)果
    NSWidthInsensitiveSearch NS_ENUM_AVAILABLE(10_5, 2_0) = 256, /* If specified, ignores width differences ('a' == UFF41) */

    //忽略不區(qū)分大小寫(xiě)比較的選項(xiàng)燕偶,并強(qiáng)制返回NSOrderedAscending 或者 NSOrderedDescending
    NSForcedOrderingSearch NS_ENUM_AVAILABLE(10_5, 2_0) = 512, /* If specified, comparisons are forced to return either NSOrderedAscending or NSOrderedDescending if the strings are equivalent but not strictly equal, for stability when sorting (e.g. "aaa" > "AAA" with NSCaseInsensitiveSearch specified) */

    ////只能應(yīng)用于 rangeOfString:..., stringByReplacingOccurrencesOfString:...和 replaceOccurrencesOfString:... 方法。使用通用兼容的比較方法础嫡,如果設(shè)置此項(xiàng)指么,可以去掉 NSCaseInsensitiveSearch 和 NSAnchoredSearch
    NSRegularExpressionSearch NS_ENUM_AVAILABLE(10_7, 3_2) = 1024    /* Applies to rangeOfString:..., stringByReplacingOccurrencesOfString:..., and replaceOccurrencesOfString:... methods only; the search string is treated as an ICU-compatible regular expression; if set, no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch */
};

```

下面看代碼。


```
    /**
     *5. - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
     *
     *  @param string:This value must not be nil. If this value is nil, the behavior is undefined and may change in future versions of macOS.
     *  @param mask:Options for the search—you can combine any of the following using a C bitwise OR operator: NSCaseInsensitiveSearch, NSLiteralSearch, NSNumericSearch. See String Programming Guide for details on these options.
     *
     *  @return :Returns an NSComparisonResult value that indicates the lexical ordering. NSOrderedAscending the receiver precedes aString in lexical ordering, NSOrderedSame the receiver and aString are equivalent in lexical value, and NSOrderedDescending if the receiver follows aString.
     *
     */
    
        //不區(qū)分大小寫(xiě)
        NSString *ocStr1 = @"AABBCC";
        NSString *ocStr2 = @"aABBCC";
        NSComparisonResult result1 = [ocStr1 compare:ocStr2 options:NSCaseInsensitiveSearch];
        NSLog(@"result1--%ld",result1);

        //嚴(yán)格區(qū)分大小寫(xiě)
        NSString *ocStr3 = @"AABBCC";
        NSString *ocStr4 = @"aABBCC";
        NSComparisonResult result2 = [ocStr3 compare:ocStr4 options:NSLiteralSearch];
        NSLog(@"result2--%ld",result2);
    
        //倒序比較
        NSString *ocStr5 = @"AABBCC";
        NSString *ocStr6 = @"aABBCA";
        NSComparisonResult result3 = [ocStr5 compare:ocStr6 options:NSBackwardsSearch];
        NSLog(@"result3--%ld",result3);
    
        //數(shù)字比較
        NSString *ocStr7 = @"AAB33BCC";
        NSString *ocStr8 = @"aAB44BCC";
        NSComparisonResult result4 = [ocStr7 compare:ocStr8 options:NSNumericSearch];
        NSLog(@"result4--%ld",result4);
```

下面看結(jié)果榴鼎。

```
2017-05-07 16:50:09.606 NSString你會(huì)用嗎伯诬?[4229:183365] result1--0
2017-05-07 16:50:09.606 NSString你會(huì)用嗎?[4229:183365] result2---1
2017-05-07 16:50:09.606 NSString你會(huì)用嗎巫财?[4229:183365] result3---1
2017-05-07 16:50:09.607 NSString你會(huì)用嗎盗似?[4229:183365] result4---1

```

**結(jié)論**:如果處理的文本是要呈現(xiàn)給用戶(hù)時(shí),這時(shí)候使用的應(yīng)該是localizedStandardCompare: 方法平项,或者使用compare:options:range:locale:赫舒,傳入用戶(hù)的locale悍及。還有這里的mask參數(shù),不一定是一個(gè)單一的枚舉值接癌,還可以是很多個(gè)枚舉值一起使用心赶,利用或"|"進(jìn)行連接使用。

-----------

### 六缺猛、- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToCompare;

??這個(gè)大家應(yīng)該很清楚缨叫,和五中的方法相比,差的就是range這一個(gè)參數(shù)荔燎,也就是說(shuō)這個(gè)方法可以比較一定區(qū)間的大小耻姥,當(dāng)range的范圍是整個(gè)字符串的時(shí)候,就可以看做和五方法是等價(jià)的了有咨。下面看代碼琐簇。

```
    /**
     *6. - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToCompare;
     *
     *  @param string:This value must not be nil. If this value is nil, the behavior is undefined and may change in future versions of macOS.
     *  @param mask:Options for the search—you can combine any of the following using a C bitwise OR operator: NSCaseInsensitiveSearch, NSLiteralSearch, NSNumericSearch. See String Programming Guide for details on these options.
     *  @param range:the range of string for comparision.
     *
     *  @return :Returns an NSComparisonResult value that indicates the lexical ordering. NSOrderedAscending the receiver precedes aString in lexical ordering, NSOrderedSame the receiver and aString are equivalent in lexical value, and NSOrderedDescending if the receiver follows aString.
     *
     */

        //不區(qū)分大小寫(xiě)
        NSString *ocStr1 = @"AABBCC";
        NSString *ocStr2 = @"AABBCA";
        NSRange range1 = NSMakeRange(0, ocStr1.length);
        NSComparisonResult result1 = [ocStr1 compare:ocStr2 options:NSCaseInsensitiveSearch range:range1];
        NSLog(@"result1==%ld",result1);
    
        NSRange range2 = NSMakeRange(0, 1);
        NSComparisonResult result2 = [ocStr1 compare:ocStr2 options:NSCaseInsensitiveSearch range:range2];
        NSLog(@"result2==%ld",result2);

```

看結(jié)果。

```

2017-05-07 17:18:28.943 NSString你會(huì)用嗎摔吏?[4669:202369] result1==1
2017-05-07 17:18:28.944 NSString你會(huì)用嗎鸽嫂?[4669:202369] result2==-1

```

**結(jié)論**:如果處理的文本是要呈現(xiàn)給用戶(hù)時(shí),這時(shí)候使用的應(yīng)該是localizedStandardCompare: 方法征讲,或者使用compare:options:range:locale:据某,傳入用戶(hù)的locale。range也不能越界诗箍,否則會(huì)報(bào)錯(cuò)癣籽,NSRangeException。

--------------

### 七滤祖、- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToCompare locale:(id)locale;

看代碼筷狼。

```

    /**
     *7. - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToCompare locale:(id)locale;
     *
     *  @param string:This value must not be nil. If this value is nil, the behavior is undefined and may change in future versions of macOS.
     *  @param mask:Options for the search—you can combine any of the following using a C bitwise OR operator: NSCaseInsensitiveSearch, NSLiteralSearch, NSNumericSearch. See String Programming Guide for details on these options.
     *  @param range:the range of string for comparision.
     *  @param locale:An instance of NSLocale. To use the current locale, pass [NSLocale currentLocale]. For example, if you are comparing strings to present to the end-user, use the current locale. To use the system locale, pass nil.
     *
     *  @return :Returns an NSComparisonResult value that indicates the lexical ordering. NSOrderedAscending the receiver precedes aString in lexical ordering, NSOrderedSame the receiver and aString are equivalent in lexical value, and NSOrderedDescending if the receiver follows aString.
     *
     */
        NSString *ocStr1 = @"AABBCC";
        NSString *ocStr2 = @"AABBCA";
        NSRange range1 = NSMakeRange(0, ocStr1.length);
        NSComparisonResult result1 = [ocStr1 compare:ocStr2 options:NSCaseInsensitiveSearch range:range1 locale:[NSLocale currentLocale]];
        NSLog(@"result1==%ld",result1);

```
看結(jié)果。

```
2017-05-07 17:26:37.468 NSString你會(huì)用嗎匠童?[4790:209130] result1==1

```
**結(jié)論**:如果處理的文本是要呈現(xiàn)給用戶(hù)時(shí)埂材,這時(shí)候使用的應(yīng)該是localizedStandardCompare: 方法,或者使用compare:options:range:locale:汤求,傳入用戶(hù)的locale俏险。range也不能越界,否則會(huì)報(bào)錯(cuò)扬绪,NSRangeException竖独。還有就是locale
這個(gè)參數(shù),使用當(dāng)?shù)丶放#蛡鱗NSLocale currentLocale]莹痢,使用系統(tǒng)的就用nil。需要注意的是locale參數(shù)影響的是相等和排序算法,例如在一些地區(qū)竞膳,重音字符要排在普通字符之后航瞭,在另外一寫(xiě)地區(qū)將他們排在"z"之后。

-------------

### 八顶猜、- (NSComparisonResult)localizedStandardCompare:(NSString *)string;

```
    /**
     *8. - (NSComparisonResult)localizedStandardCompare:(NSString *)string;
     *
     *  @param string:This value must not be nil. If this value is nil, the behavior is undefined and may change in future versions of macOS.
     *
     *  @return :Returns an NSComparisonResult value that indicates the lexical ordering. NSOrderedAscending the receiver precedes aString in lexical ordering, NSOrderedSame the receiver and aString are equivalent in lexical value, and NSOrderedDescending if the receiver follows aString.
     *
     */
    NSString *ocStr1 = @"AABBCC";
    NSString *ocStr2 = @"AABBCA";
    NSComparisonResult result1 = [ocStr1 localizedStandardCompare:ocStr2];
    NSLog(@"result1==%ld",result1);
```

看結(jié)果沧奴。

```

2017-05-07 17:35:37.829 NSString你會(huì)用嗎?[4929:214923] result1==1

```

**結(jié)論**:利用這個(gè)方法進(jìn)行排序长窄,在不同的地區(qū)排序結(jié)果是不一樣的,并且在未來(lái)發(fā)布版本的時(shí)候還可能會(huì)發(fā)生變化纲菌,這個(gè)方法的locale使用的是current locale挠日。

-------------

### 九、-  (BOOL)hasPrefix:(NSString *)str;

看代碼翰舌。

```

    /**
     *9. - (BOOL)hasPrefix:(NSString *)str;     
     *
     *  @param string:a string
     *
     *  @return :YES if aString matches the beginning characters of the receiver, otherwise NO. Returns NO if aString is empty.
     *
     */
    NSString *ocStr1 = @"AABBCC";
    
    BOOL isHasPrefix = [ocStr1 hasPrefix:@"AA"];
    NSLog(@"isHasPrefix==%d",isHasPrefix);
    
    BOOL isHasPrefix1 = [ocStr1 hasPrefix:@"AAA"];
    NSLog(@"isHasPrefix1==%d",isHasPrefix1);


```

看結(jié)果嚣潜。
```
2017-05-07 17:45:08.300 NSString你會(huì)用嗎?[5087:224155] isHasPrefix==1
2017-05-07 17:45:08.301 NSString你會(huì)用嗎椅贱?[5087:224155] isHasPrefix1==0

```

**結(jié)論**:這里就不解釋了懂算,0就是表示不以所選擇的字符串做前綴,1就表示以所選擇的字符串做前綴庇麦。需要說(shuō)的是计技,這個(gè)方法可以看做方法五中的option為 NSAnchoredSearch的特殊情況。

-------------


### 十山橄、- (BOOL)hasSuffix:(NSString *)str;

看代碼垮媒。

```
    /**
     *10. - (BOOL)hasSuffix:(NSString *)str;
     *
     *  @param string:a string
     *
     *  @return :YES if aString matches the beginning characters of the receiver, otherwise NO. Returns NO if aString is empty.
     *
     */
    NSString *ocStr1 = @"AABBCC";
    
    BOOL isHasPrefix = [ocStr1 hasSuffix:@"BCC"];
    NSLog(@"isHasPrefix==%d",isHasPrefix);
    
    BOOL isHasPrefix1 = [ocStr1 hasSuffix:@"ABCC"];
    NSLog(@"isHasPrefix1==%d",isHasPrefix1);


```

看結(jié)果。

```
2017-05-07 17:50:28.910 NSString你會(huì)用嗎航棱?[5206:228422] isHasPrefix==1
2017-05-07 17:50:28.911 NSString你會(huì)用嗎睡雇?[5206:228422] isHasPrefix1==0

```

**結(jié)論**:這里就不解釋了,0就是表示不以所選擇的字符串做后綴饮醇,1就表示以所選擇的字符串做后綴它抱。需要說(shuō)的是,這個(gè)方法可以看做方法五中的option為 NSAnchoredSearch的特殊情況朴艰。

--------------



### 十一观蓄、- (BOOL)isEqualToString:(NSString *)aString;

先看代碼。

```
    /**
     *11. - (BOOL)isEqualToString:(NSString *)aString;
     *
     *  @param aString:The string with which to compare the receiver.
     *
     *  @return :YES if aString is equivalent to the receiver (if they have the same id or if they are NSOrderedSame in a literal comparison), otherwise NO.
     *
     */
    NSString *ocStr1 = @"AABBCC";
    NSString *ocStr2 = @"AABBCD";
    BOOL isEqualStr = [ocStr1 isEqualToString:ocStr2];
    NSLog(@"isEqualStr==%d",isEqualStr);
```
看結(jié)果呵晚。

```

2017-05-07 18:09:41.126 NSString你會(huì)用嗎蜘腌?[5444:242832] isEqualStr==0

```

**結(jié)論**:這個(gè)比較是采用標(biāo)準(zhǔn)的字符串進(jìn)行比較的,也就是說(shuō)用字符串的長(zhǎng)度乘以UTF-16單位編碼長(zhǎng)度組成該字符串饵隙。當(dāng)比較時(shí)撮珠,如果單個(gè)編碼相同時(shí),字符串就是相同的,比較時(shí)比較的是UTF-16編碼單元芯急。比如“?” 表示由“O” (U+004F LATIN CAPITAL LETTER O) 和 “¨” (U+0308 COMBINING DIAERESIS) 組成勺届,不會(huì)與“?” 這個(gè)代表單個(gè)編碼字符 (U+00D6 LATIN CAPITAL LETTER O WITH DIAERESIS)進(jìn)行比較。而且如果你確定兩個(gè)比較的對(duì)象是字符串時(shí)娶耍,該方法要優(yōu)于方法isEqual:免姿。

-------------



### 十二、@property(readonly) NSUInteger hash;

一個(gè)無(wú)符號(hào)的整形數(shù)字作為哈希表的地址榕酒,看代碼胚膊。

```
    /**
     *12. @property(readonly) NSUInteger hash;
     *
     */
    
    NSString *ocStr1 = @"AABBCC";
    NSString *ocStr2 = @"AABBCC";
    NSUInteger ocStr1Hash = ocStr1.hash;
    NSUInteger ocStr2Hash = ocStr2.hash;
    NSLog(@"ocStr1Hash==%lu,ocStr2Hash==%lu",ocStr1Hash,ocStr2Hash);
    if (ocStr1Hash == ocStr2Hash) {
        NSLog(@"他倆是相等的字符串");
    }

```

看結(jié)果。

```
2017-05-07 18:22:51.766 NSString你會(huì)用嗎想鹰?[5640:252521] ocStr1Hash==6494203873124370,ocStr2Hash==6494203873124370
2017-05-07 18:22:51.766 NSString你會(huì)用嗎紊婉?[5640:252521] 他倆是相等的字符串
```

**結(jié)論**:相等的字符串擁有相同的hash值。


#  后記

> ??兩天的假期就這么結(jié)束了辑舷,這個(gè)今天就寫(xiě)這么多了喻犁,NSString剩下的以后寫(xiě),未完何缓,待續(xù)~~~


![秋](http://upload-images.jianshu.io/upload_images/3691932-cb5eb656d457385c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/500)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末肢础,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子碌廓,更是在濱河造成了極大的恐慌传轰,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件氓皱,死亡現(xiàn)場(chǎng)離奇詭異路召,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)波材,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)股淡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人廷区,你說(shuō)我怎么就攤上這事唯灵。” “怎么了隙轻?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵埠帕,是天一觀(guān)的道長(zhǎng)。 經(jīng)常有香客問(wèn)我玖绿,道長(zhǎng)敛瓷,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任斑匪,我火速辦了婚禮呐籽,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己狡蝶,他們只是感情好庶橱,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著贪惹,像睡著了一般苏章。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上奏瞬,一...
    開(kāi)封第一講書(shū)人閱讀 51,125評(píng)論 1 297
  • 那天枫绅,我揣著相機(jī)與錄音,去河邊找鬼硼端。 笑死撑瞧,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的显蝌。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼订咸,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼曼尊!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起脏嚷,我...
    開(kāi)封第一講書(shū)人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤骆撇,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后父叙,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體神郊,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年趾唱,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了涌乳。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡甜癞,死狀恐怖夕晓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情悠咱,我是刑警寧澤蒸辆,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站析既,受9級(jí)特大地震影響躬贡,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜眼坏,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一拂玻、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦纺讲、人聲如沸擂仍。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)逢渔。三九已至,卻和暖如春乡括,著一層夾襖步出監(jiān)牢的瞬間肃廓,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工诲泌, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留盲赊,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓敷扫,卻偏偏與公主長(zhǎng)得像哀蘑,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子葵第,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容