NSString簡單細說(十)—— 字串的分解
版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2017.05.10 |
前言
前面我簡單的寫了些NSString的初始化,寫了幾篇,都不難,但是可以對新手有一定的小幫助,對於大神級人物可以略過這幾篇,NSString本來就沒有難的,都是細枝末節,忘記了查一下就會了,沒有技術難點,下面我們繼續~~~
1. NSString簡單細說(一)—— NSString整體架構
2. NSString簡單細說(二)—— NSString的初始化
3. NSString簡單細說(三)—— NSString初始化
4. NSString簡單細說(四)—— 從URL初始化
5. NSString簡單細說(五)—— 向檔案或者URL寫入
6. NSString簡單細說(六)—— 字串的長度
7. NSString簡單細說(七)—— 與C字串的轉化
8. NSString簡單細說(八)—— 識別和比較字串
9. NSString簡單細說(九)—— 字串的合併
詳述
字串的分解
一、- (NSArray<NSString *> *)componentsSeparatedByString:(NSString *)separator;
看程式碼。
/**
*1. - (NSArray<NSString *> *)componentsSeparatedByString:(NSString *)separator;
*
* @param separator:The separator string.
*
* @return :An NSArray object containing substrings from the receiver that have been divided by separator.
*
*/
//例1
NSString *list = @"Karin, Carrie, David";
NSArray *listItems = [list componentsSeparatedByString:@", "];
NSLog(@"listItems--%@",listItems);
//例2
NSString *testStr = @", Norman, Stanley, Fletcher";
NSArray *testArr = [testStr componentsSeparatedByString:@","];
NSLog(@"testArr--%@",testArr);
//例3
NSString *testStr1 = @"Norman, Stanley, Fletcher";
NSArray *testArr1 = [testStr1 componentsSeparatedByString:@"#"];
NSLog(@"testArr1--%@",testArr1);
看結果。
2017-05-10 23:27:21.387 NSString你會用嗎?[1425:41922] listItems--(
Karin,
Carrie,
David
)
2017-05-10 23:27:21.387 NSString你會用嗎?[1425:41922] testArr--(
"",
" Norman",
" Stanley",
" Fletcher"
)
2017-05-10 23:27:21.387 NSString你會用嗎?[1425:41922] testArr1--(
"Norman, Stanley, Fletcher"
)
結論:這裡舉了三個例子,例1中是最常用的情況,直接被逗號隔開的有三部分,組成了一個陣列;例2中因為開頭是分割標誌逗號,所以返回陣列的第一個元素就是一個空字串,我這裡是開頭是這樣,如果字串是結尾是逗號,那麼這個陣列最後一個元素就是空字串;例3中,因為要分割的字串中並沒有分割標誌"#",那麼返回整個字串,即整個陣列就一個元素就是原字串。
二、- (NSArray<NSString *> *)componentsSeparatedByCharactersInSet:(NSCharacterSet **)separator;
看程式碼。
/**
*2. - (NSArray <NSString *> **)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator;
*
* @param separator:A character set containing the characters to to use to split the receiver. Must not be nil.
*
* @return :An NSArray object containing substrings from the receiver that have been divided by characters in separator.
*
*/
NSString *testStr = @"ssAAj23939gApjhoAss";
NSCharacterSet *character = [NSCharacterSet uppercaseLetterCharacterSet];
NSArray *resultArr = [testStr componentsSeparatedByCharactersInSet:character];
NSLog(@"%@----resultArr",resultArr);
看結果。
2017-05-10 23:39:34.014 NSString你會用嗎?[1581:50076] (
ss,
"",
j23939g,
pjho,
ss
)----resultArr
結論:這個方法就是找到已固定的character分割的字串組成的陣列,我這裡使用的是大寫字元,所以就是找到大寫字元分割的陣列。同樣,陣列中元素出現的次序和在原字串中的次序是一致的,如果字串開始或者結束是以分割的character開始或者結束的,那麼一樣在開頭或者結束產生空格,也就是說陣列中第一個元素或者最後一個元素就是空格。
三、- (NSArray<NSString *> *)componentsSeparatedByCharactersInSet:(NSCharacterSet **)separator;
看程式碼。
/**
*3. - (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set;
*
* @param set:A character set containing the characters to remove from the receiver. set must not be nil.
*
* @return :A new string made by removing from both ends of the receiver characters contained in set. If the receiver is composed entirely of characters from set, the empty string is returned.
*
*/
//例1
NSString *testStr1 = @"ssAAj23939gApjhoAss";
NSCharacterSet *character = [NSCharacterSet lowercaseLetterCharacterSet];
NSString *resultArr1 = [testStr1 stringByTrimmingCharactersInSet:character];
NSLog(@"%@----resultArr1",resultArr1);
//例2
NSString *testStr2 = @"AAAA";
NSCharacterSet *character1 = [NSCharacterSet uppercaseLetterCharacterSet];
NSString *resultArr2 = [testStr2 stringByTrimmingCharactersInSet:character1];
NSLog(@"%@----resultArr2",resultArr2);
//例3
NSString *testStr3 = @" AAAA ";
NSCharacterSet *character3 = [NSCharacterSet whitespaceCharacterSet];
NSString *resultArr3 = [testStr3 stringByTrimmingCharactersInSet:character3];
NSLog(@"%@----resultArr3",resultArr3);
看結果。
2017-05-10 23:50:12.998 NSString你會用嗎?[1721:58430] AAj23939gApjhoA----resultArr1
2017-05-10 23:50:12.998 NSString你會用嗎?[1721:58430] ----resultArr2
2017-05-10 23:50:12.999 NSString你會用嗎?[1721:58430] AAAA----resultArr3
結論:這個方法就是去除字串兩端某種固定形式的字串。例1中是去除字串兩端的小寫字母;例2中是去除字串兩端的大寫字母,但是原字串都是大寫字母,所以整個字串就是空字串了;例3中是去除字串兩端的空格。
四、- (NSString *)substringFromIndex:(NSUInteger)from;
看程式碼。
/**
*4. - (NSString *)substringFromIndex:(NSUInteger)from;
*
* @param from:An index. The value must lie within the bounds of the receiver, or be equal to the length of the receiver.
*
* @return A new string containing the characters of the receiver from the one at anIndex to the end. If anIndex is equal to the length of the string, returns an empty string.
*
*/
//例1
NSString *testStr2 = @"AAAA";
NSString *resultArr2 = [testStr2 substringFromIndex:4];
NSLog(@"%@----resultArr2",resultArr2);
//例2
NSString *testStr3 = @"39392egehekhou01GFUGGI";
NSString *resultArr3 = [testStr3 substringFromIndex:1];
NSLog(@"%@----resultArr3",resultArr3);
//例3
NSString *testStr1 = @"ssAAj23939gApjhoAss";
NSString *resultArr1 = [testStr1 substringFromIndex:30];
NSLog(@"%@----resultArr1",resultArr1);
看結果。
2017-05-11 00:02:49.761 NSString你會用嗎?[1922:66881] ----resultArr2
2017-05-11 00:02:49.761 NSString你會用嗎?[1922:66881] 9392egehekhou01GFUGGI----resultArr3
2017-05-11 00:02:49.767 NSString你會用嗎?[1922:66881] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSCFConstantString substringFromIndex:]: Index 30 out of bounds; string length 19'
*** First throw call stack:
(
0 CoreFoundation 0x00000001075d0d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010703221e objc_exception_throw + 48
2 CoreFoundation 0x000000010763a2b5 +[NSException raise:format:] + 197
3 Foundation 0x0000000106b0a88e -[NSString substringFromIndex:] + 126
4 NSString你会用吗? 0x0000000106a5b219 -[JJStringTestVC divideString] + 217
5 NSString你会用吗? 0x0000000106a5b12d -[JJStringTestVC viewDidLoad] + 189
6 UIKit 0x0000000107b96a3d -[UIViewController loadViewIfRequired] + 1258
7 UIKit 0x0000000107b9d062 -[UIViewController __viewWillAppear:] + 118
8 UIKit 0x0000000107bc81d3 -[UINavigationController _startCustomTransition:] + 1290
9 UIKit 0x0000000107bd8e48 -[UINavigationController _startDeferredTransitionIfNeeded:] + 697
10 UIKit 0x0000000107bd9fdb -[UINavigationController __viewWillLayoutSubviews] + 58
11 UIKit 0x0000000107dd0dd7 -[UILayoutContainerView layoutSubviews] + 223
12 UIKit 0x0000000107ab9ab8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
13 QuartzCore 0x000000010c353bf8 -[CALayer layoutSublayers] + 146
14 QuartzCore 0x000000010c347440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
15 QuartzCore 0x000000010c3472be _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
16 QuartzCore 0x000000010c2d5318 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
17 QuartzCore 0x000000010c3023ff _ZN2CA11Transaction6commitEv + 475
18 QuartzCore 0x000000010c302d6f _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113
19 CoreFoundation 0x0000000107575267 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
20 CoreFoundation 0x00000001075751d7 __CFRunLoopDoObservers + 391
21 CoreFoundation 0x0000000107559f8e __CFRunLoopRun + 1198
22 CoreFoundation 0x0000000107559884 CFRunLoopRunSpecific + 420
23 GraphicsServices 0x000000010b44da6f GSEventRunModal + 161
24 UIKit 0x00000001079f4c68 UIApplicationMain + 159
25 NSString你会用吗? 0x0000000106a5b04f main + 111
26 libdyld.dylib 0x000000010a4bd68d start + 1
27 ??? 0x0000000000000001 0x0 + 1
)
結論:這個字串就是得到給定索引到字串結尾的那部分字串,如果索引值和字串長度相等那麼得到的就是空字串,如例1;例2就是從索引1到末尾的那部分字串;例3由於字串的長度小於給定的索引,就是報陣列異常的錯誤。
五、- (NSString *)substringWithRange:(NSRange)range;
看程式碼。
/**
*5. - (NSString *)substringWithRange:(NSRange)range;
*
* @param range:A range. The range must not exceed the bounds of the receiver.
*
* @return :A string object containing the characters of the receiver that lie within aRange.
*
*/
//例1
NSString *testStr1 = @"AAAA";
NSRange range1 = NSMakeRange(0, 4);
NSString *resultArr1 = [testStr1 substringWithRange:range1];
NSLog(@"%@----resultArr1",resultArr1);
//例2
NSString *testStr2 = @"39392egehekhou01GFUGGI";
NSRange range2 = NSMakeRange(0, 5);
NSString *resultArr2 = [testStr2 substringWithRange:range2];
NSLog(@"%@----resultArr2",resultArr2);
//例3
NSString *testStr3 = @"ssAAj23939gApjhoAss";
NSRange range3 = NSMakeRange(0, 40);
NSString *resultArr3 = [testStr3 substringWithRange:range3];
NSLog(@"%@----resultArr3",resultArr3);
看結果。
2017-05-11 00:16:55.007 NSString你會用嗎?[2062:73678] AAAA----resultArr1
2017-05-11 00:16:55.007 NSString你會用嗎?[2062:73678] 39392----resultArr2
2017-05-11 00:16:55.015 NSString你會用嗎?[2062:73678] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFConstantString substringWithRange:]: Range {0, 40} out of bounds; string length 19'
*** First throw call stack:
(
0 CoreFoundation 0x0000000107a3bd4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010749d21e objc_exception_throw + 48
2 CoreFoundation 0x0000000107aa52b5 +[NSException raise:format:] + 197
3 CoreFoundation 0x00000001079a102b -[__NSCFString substringWithRange:] + 171
4 NSString你会用吗? 0x0000000106ec61f9 -[JJStringTestVC divideString] + 521
5 NSString你会用吗? 0x0000000106ec5fdd -[JJStringTestVC viewDidLoad] + 189
6 UIKit 0x0000000108001a3d -[UIViewController loadViewIfRequired] + 1258
7 UIKit 0x0000000108008062 -[UIViewController __viewWillAppear:] + 118
8 UIKit 0x00000001080331d3 -[UINavigationController _startCustomTransition:] + 1290
9 UIKit 0x0000000108043e48 -[UINavigationController _startDeferredTransitionIfNeeded:] + 697
10 UIKit 0x0000000108044fdb -[UINavigationController __viewWillLayoutSubviews] + 58
11 UIKit 0x000000010823bdd7 -[UILayoutContainerView layoutSubviews] + 223
12 UIKit 0x0000000107f24ab8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
13 QuartzCore 0x000000010c733bf8 -[CALayer layoutSublayers] + 146
14 QuartzCore 0x000000010c727440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
15 QuartzCore 0x000000010c7272be _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
16 QuartzCore 0x000000010c6b5318 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
17 QuartzCore 0x000000010c6e23ff _ZN2CA11Transaction6commitEv + 475
18 QuartzCore 0x000000010c6e2d6f _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113
19 CoreFoundation 0x00000001079e0267 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
20 CoreFoundation 0x00000001079e01d7 __CFRunLoopDoObservers + 391
21 CoreFoundation 0x00000001079c4f8e __CFRunLoopRun + 1198
22 CoreFoundation 0x00000001079c4884 CFRunLoopRunSpecific + 420
23 GraphicsServices 0x000000010b82da6f GSEventRunModal + 161
24 UIKit 0x0000000107e5fc68 UIApplicationMain + 159
25 NSString你会用吗? 0x0000000106ec5eff main + 111
26 libdyld.dylib 0x000000010a89d68d start + 1
27 ??? 0x0000000000000001 0x0 + 1
)
結論:這個也很簡單,不多說了。
六、- (NSString *)substringToIndex:(NSUInteger)to;
看程式碼。
/**
* 6. - (NSString *)substringToIndex:(NSUInteger)to;
*
* @param to:An index. The value must lie within the bounds of the receiver, or be equal to the length of the receiver.
*
* @return :A new string containing the characters of the receiver up to, but not including, the one at anIndex. If anIndex is equal to the length of the string, returns a copy of the receiver.
*
*/
//例1
NSString *testStr1 = @"AAAA";
NSString *resultArr1 = [testStr1 substringToIndex:3];
NSLog(@"%@----resultArr1",resultArr1);
//例2
NSString *testStr2 = @"39392egehekhou01GFUGGI";
NSString *resultArr2 = [testStr2 substringToIndex:200];
NSLog(@"%@----resultArr2",resultArr2);
看結果。
2017-05-11 00:26:37.704 NSString你會用嗎?[2228:82867] AAA----resultArr1
2017-05-11 00:26:37.709 NSString你會用嗎?[2228:82867] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSCFConstantString substringToIndex:]: Index 200 out of bounds; string length 22'
*** First throw call stack:
(
0 CoreFoundation 0x000000010c4c1d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010bf2321e objc_exception_throw + 48
2 CoreFoundation 0x000000010c52b2b5 +[NSException raise:format:] + 197
3 Foundation 0x000000010b9fb784 -[NSString substringToIndex:] + 118
4 NSString你会用吗? 0x000000010b94c25b -[JJStringTestVC divideString] + 139
5 NSString你会用吗? 0x000000010b94c1bd -[JJStringTestVC viewDidLoad] + 189
6 UIKit 0x000000010ca87a3d -[UIViewController loadViewIfRequired] + 1258
7 UIKit 0x000000010ca8e062 -[UIViewController __viewWillAppear:] + 118
8 UIKit 0x000000010cab91d3 -[UINavigationController _startCustomTransition:] + 1290
9 UIKit 0x000000010cac9e48 -[UINavigationController _startDeferredTransitionIfNeeded:] + 697
10 UIKit 0x000000010cacafdb -[UINavigationController __viewWillLayoutSubviews] + 58
11 UIKit 0x000000010ccc1dd7 -[UILayoutContainerView layoutSubviews] + 223
12 UIKit 0x000000010c9aaab8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
13 QuartzCore 0x00000001111b9bf8 -[CALayer layoutSublayers] + 146
14 QuartzCore 0x00000001111ad440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
15 QuartzCore 0x00000001111ad2be _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
16 QuartzCore 0x000000011113b318 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
17 QuartzCore 0x00000001111683ff _ZN2CA11Transaction6commitEv + 475
18 QuartzCore 0x0000000111168d6f _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113
19 CoreFoundation 0x000000010c466267 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
20 CoreFoundation 0x000000010c4661d7 __CFRunLoopDoObservers + 391
21 CoreFoundation 0x000000010c44af8e __CFRunLoopRun + 1198
22 CoreFoundation 0x000000010c44a884 CFRunLoopRunSpecific + 420
23 GraphicsServices 0x00000001102b3a6f GSEventRunModal + 161
24 UIKit 0x000000010c8e5c68 UIApplicationMain + 159
25 NSString你会用吗? 0x000000010b94c0df main + 111
26 libdyld.dylib 0x000000010f32368d start + 1
27 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
結論:該方法就是返回到指定索引的字串,但是不包括索引那一位。所以,例1返回"AAA",例2由於越界就crash了。
後記
先寫這麼多吧,晚安哈~~~~,明天還要工作。
相關文章
- JavaScript字串api簡單說明JavaScript字串API
- iOS NSString 字串的特殊處理iOS字串
- iOS擷取NSString字串iOS字串
- iOS NSString字串擷取方法iOS字串
- 字串-簡單字串排序字串排序
- 快速學習nodejs系列:十、簡單說說nodejs官方APINodeJSAPI
- 字串-簡單字串比較字串
- 改進JAVA字串分解的方法Java字串
- 簡單的字串處理字串
- 簡單說說Restful APIRESTAPI
- 細說new和malloc的十大區別
- 誰說 JavaScript 簡單的?JavaScript
- 簡單說說iOS之WKWebView的用法iOSWebView
- 簡單說說webpack的配置檔案Web
- BZOJ4962 : 簡單的字串字串
- jquery datatables各引數詳細說明及簡單應用jQuery
- 先簡單說一說Java中的CopyOnWriteArrayListJava
- 簡單說說U-boot的修改(轉)boot
- java字串的簡單介紹(轉)Java字串
- github的詳細使用,非常簡單!Github
- 一個簡單的字串查詢程式字串
- 簡單說說ES6新特性
- 簡單說說linux關機命令Linux
- 簡單說說PHP優化那些事PHP優化
- 簡單說區塊鏈區塊鏈
- sqlmap簡單中文說明SQL
- Redis—簡單動態字串(SDS)Redis字串
- 細說 Angular 的自定義表單控制元件Angular控制元件
- pycharm下與spark的互動 詳細配置說明以及spark 執行簡單例子PyCharmSpark單例
- 細說 Angular 2+ 的表單(二):響應式表單Angular
- 矩陣分解--超詳細解讀矩陣
- 簡單說兩句 Like 的優化優化
- 開工大吉!簡單的說說公司的開發規範
- MySQL實戰 | 06/07 簡單說說MySQL中的鎖MySql
- 細說駭客和普通程式設計師的十大區別程式設計師
- Object C學習筆記8-字串NSString之二Object筆記字串
- Object C學習筆記7-字串NSString之一Object筆記字串
- jarsigner 簡單使用說明JAR