版本記錄
版本號(hào) | 時(shí)間 |
---|---|
V1.0 | 2017.05.18 |
前言
前面我簡(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)化
8. NSString簡(jiǎn)單細(xì)說(shuō)(八)—— 識(shí)別和比較字符串
9. NSString簡(jiǎn)單細(xì)說(shuō)(九)—— 字符串的合并
10. NSString簡(jiǎn)單細(xì)說(shuō)(十)—— 字符串的分解
字符串的查找
一、- (BOOL)containsString:(NSString *)str;
看代碼
/**
* 1. - (BOOL)containsString:(NSString *)str;;
*
* @param str:The string to search for. This value must not be nil.
*
* @return :YES if the receiver contains str, otherwise NO.
*
*/
NSString *testStr = @"AAAasdWIU";
BOOL isContain = [testStr containsString:@"asd"];
NSLog(@"%d--isContain",isContain);
BOOL isContain1 = [testStr containsString:@"asD"];
NSLog(@"%d--isContain1",isContain1);
看結(jié)果
2017-05-18 22:52:39.361 NSString你會(huì)用嗎舵变?[984:24027] 1--isContain
2017-05-18 22:52:39.361 NSString你會(huì)用嗎酣溃?[984:24027] 0--isContain1
結(jié)論:從上面可以看到:
- 這個(gè)包含的判斷是有大小寫(xiě)之分的瘦穆,也就是casesensitive的纪隙。
- 這個(gè)方法其實(shí)和rangeOfString:options: 方法中不傳option的時(shí)候是等價(jià)的。
- 當(dāng)用于和用戶端呈現(xiàn)的時(shí)候需要使用localizedStandardContainsString: 或者localizedCaseInsensitiveContainsString: 方法扛或。
二绵咱、- (BOOL)localizedCaseInsensitiveContainsString:(NSString *)str;
先看代碼
/**
* 2. - (BOOL)localizedCaseInsensitiveContainsString:(NSString *)str;
*
* @param str:The string to search for. This value must not be nil.
*
* @return :YES if the receiver contains str, otherwise NO.
*
*/
NSString *testStr = @"AAAasdWIU";
BOOL isContain = [testStr localizedCaseInsensitiveContainsString:@"asd"];
NSLog(@"%d--isContain",isContain);
BOOL isContain1 = [testStr localizedCaseInsensitiveContainsString:@"asD"];
NSLog(@"%d--isContain",isContain1);
看結(jié)果
2017-05-18 23:04:13.618 NSString你會(huì)用嗎?[1138:32102] 1--isContain
2017-05-18 23:04:13.618 NSString你會(huì)用嗎熙兔?[1138:32102] 1--isContain
結(jié)論:由上可知:
- 這個(gè)是不區(qū)分大小寫(xiě)的悲伶,也就是說(shuō)是CaseInsensitive的艾恼。
三、- (BOOL)localizedStandardContainsString:(NSString *)str;
看代碼
/**
* 3. - (BOOL)localizedStandardContainsString:(NSString *)str;
*
* @param str:The string to search for. This value must not be nil.
*
* @return :YES if the receiver contains str, otherwise NO.
*
*/
NSString *testStr = @"AAAasdWIU";
BOOL isContain = [testStr localizedStandardContainsString:@"asd"];
NSLog(@"%d--isContain",isContain);
BOOL isContain1 = [testStr localizedStandardContainsString:@"asD"];
NSLog(@"%d--isContain",isContain1);
看結(jié)果
2017-05-18 23:14:39.764 NSString你會(huì)用嗎麸锉?[1285:42492] 1--isContain
2017-05-18 23:14:39.764 NSString你會(huì)用嗎钠绍?[1285:42492] 1--isContain
結(jié)論:這個(gè)方法也是不缺分大小寫(xiě)字母的。
四花沉、- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)searchSet;
看代碼
/**
* 4. - (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)searchSet;
*
* @param searchSet:A character set. This value must not be nil.
*
* @return :The range in the receiver of the first character found from aSet. Returns a range of {NSNotFound, 0} if none of the characters in aSet are found.
*
*/
NSString *testStr = @"AAAasdWIU";
NSCharacterSet *characterSet = [NSCharacterSet lowercaseLetterCharacterSet];
NSRange range = [testStr rangeOfCharacterFromSet:characterSet];
NSLog(@"range--location=%ld--lengh=%ld",range.location,range.length);
NSCharacterSet *characterSet1 = [NSCharacterSet uppercaseLetterCharacterSet];
NSRange range1 = [testStr rangeOfCharacterFromSet:characterSet1];
NSLog(@"range1--location=%ld--lengh=%ld",range1.location,range1.length);
看結(jié)果
2017-05-18 23:26:24.941 NSString你會(huì)用嗎柳爽?[1457:52949] range--location=3--lengh=1
2017-05-18 23:26:24.941 NSString你會(huì)用嗎?[1457:52949] range1--location=0--lengh=1
結(jié)論:由上可知:
- 這個(gè)方法的作用就是找到character所對(duì)應(yīng)的range碱屁。
- searchSet不能為空磷脯,如為空就會(huì)拋出異常NSInvalidArgumentException。
- 如果找不到就會(huì)返回結(jié)果{NSNotFound, 0}娩脾。
- 這個(gè)方法和 rangeOfCharacterFromSet:options:中options為nil時(shí)是一樣的赵誓。
五、- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)searchSet options:(NSStringCompareOptions)mask;
看代碼
/**
* 5. - (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)searchSet options:(NSStringCompareOptions)mask;
*
* @param searchSet:A character set. This value must not be nil.
* @param mask:A character set. This value must not be nil.
*
* @return :A mask specifying search options. The following options may be specified by combining them with the C bitwise OR operator: NSAnchoredSearch, NSBackwardsSearch.
*
*/
NSString *testStr = @"AAasdWIU";
NSCharacterSet *characterSet = [NSCharacterSet lowercaseLetterCharacterSet];
NSRange range = [testStr rangeOfCharacterFromSet:characterSet options:NSBackwardsSearch];
NSLog(@"range--location=%ld--lengh=%ld",range.location,range.length);
NSCharacterSet *characterSet1 = [NSCharacterSet uppercaseLetterCharacterSet];
NSRange range1 = [testStr rangeOfCharacterFromSet:characterSet1 options:NSLiteralSearch];
NSLog(@"range1--location=%ld--lengh=%ld",range1.location,range1.length);
看結(jié)果
2017-05-18 23:50:38.672 NSString你會(huì)用嗎柿赊?[1696:69877] range--location=4--lengh=1
2017-05-18 23:50:38.672 NSString你會(huì)用嗎俩功?[1696:69877] range1--location=0--lengh=1
結(jié)論:由上可知
- searchSet為空則會(huì)拋出異常。
- 這個(gè)方法可以看做方法rangeOfCharacterFromSet:options:range:碰声,range為整個(gè)字符串的特例绑雄。
六、- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)searchSet options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToSearch;
看代碼
/**
* 6. - (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)searchSet options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToSearch;
*
* @param searchSet:A character set. This value must not be nil.
* @param mask:A character set. This value must not be nil.
* @param rangeOfReceiverToSearch:A character set. This value must not be nil.
*
* @return :The range in the receiver of the first character found from aSet within aRange. Returns a range of {NSNotFound, 0} if none of the characters in aSet are found.
*
*/
NSString *testStr = @"2AasdWAI3U";
NSCharacterSet *characterSet = [NSCharacterSet uppercaseLetterCharacterSet];
NSRange strRange = NSMakeRange(0, 4);
NSRange range = [testStr rangeOfCharacterFromSet:characterSet options:NSCaseInsensitiveSearch range:strRange];
NSLog(@"range--location=%ld--lengh=%ld",range.location,range.length);
看結(jié)果
2017-05-19 00:04:17.438 NSString你會(huì)用嗎奥邮?[1885:80771] range--location=1--lengh=1
結(jié)論:這里需要注意這個(gè)例子
- searching the string “strüdel”—containing the decomposed characters “u” (U+0075 LATIN SMALL LETTER U) and “¨” (U+0308 COMBINING DIAERESIS)—with a character set containing the precomposed character “ü” (U+00FC LATIN SMALL LETTER U WITH DIAERESIS) would return the range {NSNotFound, 0}, because none of the characters in the set are found.
七万牺、- (NSRange)rangeOfString:(NSString *)searchString;
看代碼
/**
* 7. - (NSRange)rangeOfString:(NSString *)searchString;
*
* @param searchString:The string to search for.
*
* @return :An NSRange structure giving the location and length in the receiver of the first occurrence of searchString.Returns {NSNotFound, 0} if searchString is not found or is empty ("").
*
*/
NSString *testStr = @"2AasdWAI3U";
NSRange range = [testStr rangeOfString:@"as"];
NSLog(@"range--location=%ld--lengh=%ld",range.location,range.length);
看結(jié)果
2017-05-19 00:12:35.795 NSString你會(huì)用嗎?[1975:86532] range--location=2--lengh=2
結(jié)論:由此可知
- NSString對(duì)象的比較都是檢查它們的點(diǎn)序列的unicode是否相等洽腺,所以返回的searchString的區(qū)間range是不同的脚粟。所以字符序列相等的字符串也可能長(zhǎng)度是不同的。
- 當(dāng)要呈現(xiàn)給用戶的時(shí)候蘸朋,用這個(gè)方法localizedStandardRangeOfString:核无。
八、- (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask;
看代碼
/**
* 8. - (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask;
*
* @param searchString:The string to search for.
* @param mask:A mask specifying search options. For possible values, see NSStringCompareOptions.
*
* @return :An NSRange structure giving the location and length in the receiver of the first occurrence of searchString.Returns {NSNotFound, 0} if searchString is not found or is empty ("").
*
*/
NSString *testStr = @"2AasdWAI3U";
NSRange range = [testStr rangeOfString:@"2" options:NSNumericSearch];
NSLog(@"range--location=%ld--lengh=%ld",range.location,range.length);
看結(jié)果
2017-05-19 00:27:03.481 NSString你會(huì)用嗎藕坯?[2205:98656] range--location=0--lengh=1
結(jié)論:這個(gè)很簡(jiǎn)單团南,就不多說(shuō)了。
九炼彪、- (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToSearch;
看代碼
/**
* 9. - (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToSearch;
*
* @param searchString:The string to search for.
* @param mask:A mask specifying search options. For possible values, see NSStringCompareOptions.
* @param rangeOfReceiverToSearch:The range within the receiver for which to search for aString.Raises an NSRangeException if rangeOfReceiverToSearch is invalid.
*
* @return :An NSRange structure giving the location and length in the receiver of searchString
within rangeOfReceiverToSearch in the receiver, modulo the options in mask. The range returned is relative to the start of the string, not to the passed-in range. Returns {NSNotFound, 0} if searchString is not found or is empty ("").
*
*/
NSString *testStr = @"2AasdWAI3U";
NSRange range = NSMakeRange(0, 4);
NSRange range1 = [testStr rangeOfString:@"3" options:NSNumericSearch range:range];
NSLog(@"range1--location=%ld--lengh=%ld",range1.location,range1.length);
看結(jié)果
2017-05-19 00:34:17.048 NSString你會(huì)用嗎吐根?[2301:104399] range1--location=9223372036854775807--lengh=0
結(jié)論:這個(gè)很簡(jiǎn)單。當(dāng)要呈現(xiàn)給用戶的時(shí)候辐马,應(yīng)該使用方法localizedStandardRangeOfString:
十拷橘、- (NSRange)rangeOfString:(NSString ***)searchString options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToSearch locale:(NSLocale **)locale;
這個(gè)方法參數(shù)比較多,先看一下參數(shù)配置吧。
看代碼
/**
* 10. - (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToSearch locale:(NSLocale *)locale;
*
* @param searchString:The string to search for.
* @param mask:A mask specifying search options. For possible values, see NSStringCompareOptions.
* @param rangeOfReceiverToSearch:The range within the receiver for which to search for aString.Raises an NSRangeException if rangeOfReceiverToSearch is invalid.
* @param locale:The locale to use when comparing the receiver with aString. To use the current locale, pass [NSLocale currentLocale]. To use the system locale, pass nil.
The locale argument affects the equality checking algorithm. For example, for the Turkish locale, case-insensitive compare matches “I” to “?” (U+0131 LATIN SMALL DOTLESS I), not the normal “i” character.
*
* @return :An NSRange structure giving the location and length in the receiver of aString within aRange in the receiver, modulo the options in mask. The range returned is relative to the start of the string, not to the passed-in range. Returns {NSNotFound, 0} if aString is not found or is empty ("").
*
*/
NSString *testStr = @"2AasdWAI3U";
NSRange range = NSMakeRange(0, 4);
NSRange range1 = [testStr rangeOfString:@"3" options:NSNumericSearch range:range locale:nil];
NSLog(@"range1--location=%ld--lengh=%ld",range1.location,range1.length);
看結(jié)果
2017-05-19 00:44:22.482 NSString你會(huì)用嗎冗疮?[2422:112275] range1--location=9223372036854775807--lengh=0
結(jié)論:自己看代碼和過(guò)程萄唇。當(dāng)呈現(xiàn)給用戶時(shí),要使用下面這個(gè)方法 localizedStandardRangeOfString:
十一术幔、- (NSRange)localizedStandardRangeOfString:(NSString *)str;
看代碼
/**
* 11. - (NSRange)localizedStandardRangeOfString:(NSString *)str;
*
* @param str:The string to search for. This value must not be nil.
*
* @return :An NSRange structure giving the location and length in the receiver of aString within aRange in the receiver, modulo the options in mask. The range returned is relative to the start of the string, not to the passed-in range. Returns {NSNotFound, 0} if aString is not found or is empty ("").
*
*/
NSString *testStr = @"2AasdWAI3U";
NSRange range1 = [testStr localizedStandardRangeOfString:@"aa"];
NSLog(@"range1--location=%ld--lengh=%ld",range1.location,range1.length);
看結(jié)果
2017-05-19 00:50:58.881 NSString你會(huì)用嗎另萤?[2520:117628] range1--location=1--lengh=2
結(jié)論:這里的比較就是不區(qū)分大小寫(xiě)字母的比較霎苗。
十二拔恰、- (void)enumerateLinesUsingBlock:(void (^)(NSString **line, BOOL ***stop))block;
這個(gè)方法其實(shí)就是枚舉器的使用廊营,數(shù)組和字典都有這個(gè)類似方法仔燕。這個(gè)方法與 (for(... in ...)在效率上基本一致毡鉴,有時(shí)會(huì)快些我們先看一下block這個(gè)參數(shù)
我們先準(zhǔn)備個(gè)文件
看代碼
/**
* 12. - (void)enumerateLinesUsingBlock:(void (^)(NSString *line, BOOL *stop))block;
*
* @param block:The block executed for the enumeration.The block takes two arguments:
*
*/
//字符串
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSString *fileStr = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
//一行一行的讀取
[fileStr enumerateLinesUsingBlock:^(NSString * _Nonnull line, BOOL * _Nonnull stop) {
NSLog(@"%@---line",line);
}];
看結(jié)果
2017-05-19 01:03:16.160 NSString你會(huì)用嗎敢靡?[2780:127173] AASsswj1wjihoi`w`9897298`7298`2@&#&%#*(&)!&)!AASsswj---line
2017-05-19 01:03:16.160 NSString你會(huì)用嗎畸冲?[2780:127173] 1wjihoi`w`9897298`7298`2@&#&%#*(&)!&)!---line
結(jié)論:枚舉器的使用财异。
十三毒嫡、- (void)enumerateSubstringsInRange:(NSRange)range options:(NSStringEnumerationOptions)opts usingBlock:(void (^)(NSString **substring, NSRange substringRange, NSRange enclosingRange, BOOL ***stop))block;
看參數(shù)癌蚁,如下圖
這里還有個(gè)枚舉值NSStringEnumerationOptions
typedef NS_OPTIONS(NSUInteger, NSStringEnumerationOptions) {
// Pass in one of the "By" options:
NSStringEnumerationByLines = 0, // Equivalent to lineRangeForRange:
NSStringEnumerationByParagraphs = 1, // Equivalent to paragraphRangeForRange:
NSStringEnumerationByComposedCharacterSequences = 2, // Equivalent to rangeOfComposedCharacterSequencesForRange:
NSStringEnumerationByWords = 3,
NSStringEnumerationBySentences = 4,
// ...and combine any of the desired additional options:
NSStringEnumerationReverse = 1UL << 8,
NSStringEnumerationSubstringNotRequired = 1UL << 9,
NSStringEnumerationLocalized = 1UL << 10 // User's default locale
};
看代碼
/**
* 13. - (void)enumerateSubstringsInRange:(NSRange)range options:(NSStringEnumerationOptions)opts usingBlock:(void (^)(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop))block;
*
* @param range:The range within the string to enumerate substrings.
* @param opts:Options specifying types of substrings and enumeration styles.
* @param block:The block executed for the enumeration.The block takes four arguments.
*
*/
//字符串
NSString *fileStr = @"212ehohgivieye0ohsoIFIFUFYDST";
NSRange range = NSMakeRange(0, 10);
//一個(gè)字符一字符的讀取
[fileStr enumerateSubstringsInRange:range options:NSStringEnumerationByWords usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
NSLog(@"%@--substring",substring);
}];
看結(jié)果
2017-05-19 01:22:41.689 NSString你會(huì)用嗎?[3266:146676] 212ehohgiv--substring
結(jié)論:需要注意
- If this method is sent to an instance of NSMutableString, mutation (deletion, addition, or change) is allowed, as long as it is within enclosingRange. After a mutation, the enumeration continues with the range immediately following the processed range, after the length of the processed range is adjusted for the mutation. (The enumerator assumes any change in length occurs in the specified range.)
- For example, if the block is called with a range starting at location N, and the block deletes all the characters in the supplied range, the next call will also pass N as the index of the range. This is the case even if mutation of the previous range changes the string in such a way that the following substring would have extended to include the already enumerated range. For example, if the string "Hello World" is enumerated via words, and the block changes "Hello " to "Hello", thus forming "HelloWorld", the next enumeration will return "World" rather than "HelloWorld".
后記
很晚了兜畸,睡覺(jué)了努释。待續(xù)~~~~~