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了。

后记

先写这么多吧,晚安哈~~~~,明天还要工作。

繁花似锦
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,185评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,445评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,684评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,564评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,681评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,874评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,025评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,761评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,217评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,545评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,694评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,351评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,988评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,778评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,007评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,427评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,580评论 2 349

推荐阅读更多精彩内容