NSString简单细说(十二)—— 字符串的替换

96
刀客传奇
2017.05.25 23:20* 字数 605

版本记录

版本号 时间
V1.0 2017.05.25

前言

前面我简单的写了些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简单细说(九)—— 字符串的合并
10. NSString简单细说(十)—— 字符串的分解
11. NSString简单细说(十一)—— 字符串的查找

字符串的替换

一、- (NSString ***)stringByReplacingOccurrencesOfString:(NSString ****)target withString:(NSString ***)replacement;

直接看代码

    /**
     * 1. - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement;
     *
     *  @param target:The string to replace.
     *  @param replacement:The string with which to replace target.
     *
     *  @reuturn: A new string in which all occurrences of target in the receiver are replaced by replacement.
     */
    
    //字符串
    NSString *testStr = @"212eAhohgivAieye0oAhsoIFIFUFAYDSAT";
    NSString *resultStr = [testStr stringByReplacingOccurrencesOfString:@"A" withString:@"--"];
    NSLog(@"resultStr = %@",resultStr);

看输出结果

2017-05-25 01:14:25.253 NSString你会用吗?[3122:140209] resultStr = 212e--hohgiv--ieye0o--hsoIFIFUF--YDS--T

结论:简单且容易理解。


二、- (NSString *****)stringByReplacingOccurrencesOfString:(NSString *****)target withString:(NSString ***)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange;

这个方法有四个参数我们先看一下参数:

方法参数

这里参数NSStringCompareOptions是一个枚举,是对替换方法的选项参数进行配置的。这个其实大家都很熟悉了,在说到字符串的比较时,说过这个枚举值,这里只给出大家列出来,自己看吧。如果不考虑这个参数的限制可以传入0。


typedef NS_OPTIONS(NSUInteger, NSStringCompareOptions) {
    NSCaseInsensitiveSearch = 1,
    NSLiteralSearch = 2,        /* Exact character-by-character equivalence */
    NSBackwardsSearch = 4,      /* Search from end of source string */
    NSAnchoredSearch = 8,       /* Search is limited to start (or end, if NSBackwardsSearch) of source string */
    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 */
    NSDiacriticInsensitiveSearch NS_ENUM_AVAILABLE(10_5, 2_0) = 128, /* If specified, ignores diacritics (o-umlaut == o) */
    NSWidthInsensitiveSearch NS_ENUM_AVAILABLE(10_5, 2_0) = 256, /* If specified, ignores width differences ('a' == UFF41) */
    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) */
    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 */
};

下面还是直接看代码。

    /**
     * 2. - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange;
     *
     *  @param target:The string to replace.
     *  @param replacement:The string with which to replace target.
     *  @param options:A mask of options to use when comparing target with the receiver. Pass 0 to specify no options.
     *  @param searchRange:The range in the receiver in which to search for target.
     *
     *  @reuturn: A new string in which all occurrences of target, matched using options, in searchRange of the receiver are replaced by replacement.
     */
    
    //例1
    NSString *testStr1 = @"2a12eAhohgivAieye0oAhsoIaFIFUFAYDSAT";
    NSString *resultStr1 = [testStr1 stringByReplacingOccurrencesOfString:@"A" withString:@"--" options:NSCaseInsensitiveSearch range:NSMakeRange(0, testStr1.length)];
    NSLog(@"resultStr1 = %@",resultStr1);
    
    //例2
    NSString *testStr = @"2a12eAhohgivAieye0oAhsoIaFIFUFAYDSAT";
    NSString *resultStr = [testStr stringByReplacingOccurrencesOfString:@"A" withString:@"--" options:NSCaseInsensitiveSearch range:NSMakeRange(0, 5)];
    NSLog(@"resultStr = %@",resultStr);

看输出结果

2017-05-25 23:02:15.045 NSString你会用吗?[1330:37453] resultStr1 = 2--12e--hohgiv--ieye0o--hsoI--FIFUF--YDS--T
2017-05-25 23:02:15.046 NSString你会用吗?[1330:37453] resultStr = 2--12eAhohgivAieye0oAhsoIaFIFUFAYDSAT

结论:注意例1中是不区分大小写的全部长度字符串的替换,例2中是不区分大小写的length = 5的字符串的替换。


三、- (NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement;

直接看代码吧。

    /**
     * 3. - (NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString ***)replacement;
     *
     *  @param range:A range of characters in the receiver
     *  @param replacement:The string with which to replace target.
     *
     *  @reuturn: A new string in which the characters in range of the receiver are replaced by replacement.
     */
    
    //例1
    NSString *testStr1 = @"2a12eAhohgivAieye0oAhsoIaFIFUFAYDSAT";
    NSRange range1 = NSMakeRange(0, testStr1.length);
    NSString *resultStr1 = [testStr1 stringByReplacingCharactersInRange:range1 withString:@"--"];
    NSLog(@"resultStr1 = %@",resultStr1);
    
    //例2
    NSString *testStr = @"2a12eAhohgivAieye0oAhsoIaFIFUFAYDSAT";
    NSRange range = NSMakeRange(0, 2);
    NSString *resultStr = [testStr stringByReplacingCharactersInRange:range withString:@"--"];
    NSLog(@"resultStr = %@",resultStr);

看输出结果

2017-05-25 23:12:56.140 NSString你会用吗?[1463:43995] resultStr1 = --
2017-05-25 23:12:56.141 NSString你会用吗?[1463:43995] resultStr = --12eAhohgivAieye0oAhsoIaFIFUFAYDSAT

结论:这个很简单了吧,都能看懂,例1就是将全部长度的字符串替换为指定的字符串,例2是将指定长度和位置的字符串替换为指定的字符串。

后记

慢慢来,一个一个的大家说,未完,待续~~~

思念
OC