版本记录
版本号 | 时间 |
---|---|
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了。
后记
先写这么多吧,晚安哈~~~~,明天还要工作。