NSMutableString 与 $ 的矛盾

NSMutableString说: $ 把我坑惨了

上篇刚介绍了NSMutableString的使用场景,今天就遇到坑了。

之前在使用NSString的时候,并不会因为替换字符串中含有 $ 而出现问题。

测试代码:

- (void)testNSString
{
    for (int i = 0; i < 10; i++) {
        NSString *string = [[@"$" stringByAppendingString:[@(i) stringValue]] stringByAppendingString:@"$"];
        NSLog(@"string -- %@", string);
        NSString *result = @"#测试#";
        result = [result stringByReplacingOccurrencesOfString:@"测试" withString:string];
        NSLog(@"result -- %@", result);
    }

    NSArray *array = @[
                       @"a",
                       @"b",
                       @"c",
                       @"d",
                       @"e",
                       @"f",
                       @"g",
                       @"h",
                       @"i",
                       @"j",
                       @"k",
                       @"l",
                       @"m",
                       @"n",
                       @"o",
                       @"p",
                       @"q",
                       @"r",
                       @"s",
                       @"t",
                       @"u",
                       @"v",
                       @"w",
                       @"x",
                       @"y",
                       @"z",
                       ];

    for (NSString *s in array) {
        NSString *string = [[@"$" stringByAppendingString:s] stringByAppendingString:@"$"];
        NSLog(@"string -- %@", string);
        NSString *result = @"#测试#";
        result = [result stringByReplacingOccurrencesOfString:@"测试" withString:string];
        NSLog(@"result -- %@", result);
    }

    NSArray *ARRAY = @[
                       @"A",
                       @"B",
                       @"C",
                       @"D",
                       @"E",
                       @"F",
                       @"G",
                       @"H",
                       @"I",
                       @"J",
                       @"K",
                       @"L",
                       @"M",
                       @"N",
                       @"O",
                       @"P",
                       @"Q",
                       @"R",
                       @"S",
                       @"T",
                       @"U",
                       @"V",
                       @"W",
                       @"X",
                       @"Y",
                       @"Z",
                       ];

    for (NSString *s in ARRAY) {
        NSString *string = [[@"$" stringByAppendingString:s] stringByAppendingString:@"$"];
        NSLog(@"string -- %@", string);
        NSString *result = @"#测试#";
        result = [result stringByReplacingOccurrencesOfString:@"测试" withString:string];
        NSLog(@"result -- %@", result);
    }

    // 打印日志:
    /*
     2018-08-29 14:59:16.483206+0800 CouponPurchase[26186:6648795] string -- $0$
     2018-08-29 14:59:16.483851+0800 CouponPurchase[26186:6648795] result -- #$0$#
     2018-08-29 14:59:16.484575+0800 CouponPurchase[26186:6648795] string -- $1$
     2018-08-29 14:59:16.485154+0800 CouponPurchase[26186:6648795] result -- #$1$#
     2018-08-29 14:59:16.486395+0800 CouponPurchase[26186:6648795] string -- $2$
     2018-08-29 14:59:16.487165+0800 CouponPurchase[26186:6648795] result -- #$2$#
     2018-08-29 14:59:16.487729+0800 CouponPurchase[26186:6648795] string -- $3$
     2018-08-29 14:59:16.489648+0800 CouponPurchase[26186:6648795] result -- #$3$#
     2018-08-29 14:59:16.490195+0800 CouponPurchase[26186:6648795] string -- $4$
     2018-08-29 14:59:16.492352+0800 CouponPurchase[26186:6648795] result -- #$4$#
     2018-08-29 14:59:16.492940+0800 CouponPurchase[26186:6648795] string -- $5$
     2018-08-29 14:59:16.493535+0800 CouponPurchase[26186:6648795] result -- #$5$#
     2018-08-29 14:59:16.494096+0800 CouponPurchase[26186:6648795] string -- $6$
     2018-08-29 14:59:16.494676+0800 CouponPurchase[26186:6648795] result -- #$6$#
     2018-08-29 14:59:16.495277+0800 CouponPurchase[26186:6648795] string -- $7$
     2018-08-29 14:59:16.495882+0800 CouponPurchase[26186:6648795] result -- #$7$#
     2018-08-29 14:59:16.500287+0800 CouponPurchase[26186:6648795] string -- $8$
     2018-08-29 14:59:16.501060+0800 CouponPurchase[26186:6648795] result -- #$8$#
     2018-08-29 14:59:16.501929+0800 CouponPurchase[26186:6648795] string -- $9$
     2018-08-29 14:59:16.502432+0800 CouponPurchase[26186:6648795] result -- #$9$#
     2018-08-29 14:59:16.503333+0800 CouponPurchase[26186:6648795] string -- $a$
     2018-08-29 14:59:16.504046+0800 CouponPurchase[26186:6648795] result -- #$a$#
     2018-08-29 14:59:16.504556+0800 CouponPurchase[26186:6648795] string -- $b$
     2018-08-29 14:59:16.505162+0800 CouponPurchase[26186:6648795] result -- #$b$#
     2018-08-29 14:59:16.507676+0800 CouponPurchase[26186:6648795] string -- $c$
     2018-08-29 14:59:16.509516+0800 CouponPurchase[26186:6648795] result -- #$c$#
     2018-08-29 14:59:16.510337+0800 CouponPurchase[26186:6648795] string -- $d$
     2018-08-29 14:59:16.510480+0800 CouponPurchase[26186:6648795] result -- #$d$#
     2018-08-29 14:59:16.511035+0800 CouponPurchase[26186:6648795] string -- $e$
     2018-08-29 14:59:16.511459+0800 CouponPurchase[26186:6648795] result -- #$e$#
     2018-08-29 14:59:16.511762+0800 CouponPurchase[26186:6648795] string -- $f$
     2018-08-29 14:59:16.512213+0800 CouponPurchase[26186:6648795] result -- #$f$#
     2018-08-29 14:59:16.512398+0800 CouponPurchase[26186:6648795] string -- $g$
     2018-08-29 14:59:16.512869+0800 CouponPurchase[26186:6648795] result -- #$g$#
     2018-08-29 14:59:16.513236+0800 CouponPurchase[26186:6648795] string -- $h$
     2018-08-29 14:59:16.513641+0800 CouponPurchase[26186:6648795] result -- #$h$#
     2018-08-29 14:59:16.514078+0800 CouponPurchase[26186:6648795] string -- $i$
     2018-08-29 14:59:16.515079+0800 CouponPurchase[26186:6648795] result -- #$i$#
     2018-08-29 14:59:16.515585+0800 CouponPurchase[26186:6648795] string -- $j$
     2018-08-29 14:59:16.515993+0800 CouponPurchase[26186:6648795] result -- #$j$#
     2018-08-29 14:59:16.516444+0800 CouponPurchase[26186:6648795] string -- $k$
     2018-08-29 14:59:16.516881+0800 CouponPurchase[26186:6648795] result -- #$k$#
     2018-08-29 14:59:16.517219+0800 CouponPurchase[26186:6648795] string -- $l$
     2018-08-29 14:59:16.517629+0800 CouponPurchase[26186:6648795] result -- #$l$#
     2018-08-29 14:59:16.518503+0800 CouponPurchase[26186:6648795] string -- $m$
     2018-08-29 14:59:16.519644+0800 CouponPurchase[26186:6648795] result -- #$m$#
     2018-08-29 14:59:16.520393+0800 CouponPurchase[26186:6648795] string -- $n$
     2018-08-29 14:59:16.521797+0800 CouponPurchase[26186:6648795] result -- #$n$#
     2018-08-29 14:59:16.522463+0800 CouponPurchase[26186:6648795] string -- $o$
     2018-08-29 14:59:16.522993+0800 CouponPurchase[26186:6648795] result -- #$o$#
     2018-08-29 14:59:16.523506+0800 CouponPurchase[26186:6648795] string -- $p$
     2018-08-29 14:59:16.523915+0800 CouponPurchase[26186:6648795] result -- #$p$#
     2018-08-29 14:59:16.524334+0800 CouponPurchase[26186:6648795] string -- $q$
     2018-08-29 14:59:16.524569+0800 CouponPurchase[26186:6648795] result -- #$q$#
     2018-08-29 14:59:16.525381+0800 CouponPurchase[26186:6648795] string -- $r$
     2018-08-29 14:59:16.525727+0800 CouponPurchase[26186:6648795] result -- #$r$#
     2018-08-29 14:59:16.526166+0800 CouponPurchase[26186:6648795] string -- $s$
     2018-08-29 14:59:16.526614+0800 CouponPurchase[26186:6648795] result -- #$s$#
     2018-08-29 14:59:16.527147+0800 CouponPurchase[26186:6648795] string -- $t$
     2018-08-29 14:59:16.527576+0800 CouponPurchase[26186:6648795] result -- #$t$#
     2018-08-29 14:59:16.528048+0800 CouponPurchase[26186:6648795] string -- $u$
     2018-08-29 14:59:16.528406+0800 CouponPurchase[26186:6648795] result -- #$u$#
     2018-08-29 14:59:16.528668+0800 CouponPurchase[26186:6648795] string -- $v$
     2018-08-29 14:59:16.529070+0800 CouponPurchase[26186:6648795] result -- #$v$#
     2018-08-29 14:59:16.529457+0800 CouponPurchase[26186:6648795] string -- $w$
     2018-08-29 14:59:16.529968+0800 CouponPurchase[26186:6648795] result -- #$w$#
     2018-08-29 14:59:16.530375+0800 CouponPurchase[26186:6648795] string -- $x$
     2018-08-29 14:59:16.530735+0800 CouponPurchase[26186:6648795] result -- #$x$#
     2018-08-29 14:59:16.530918+0800 CouponPurchase[26186:6648795] string -- $y$
     2018-08-29 14:59:16.531378+0800 CouponPurchase[26186:6648795] result -- #$y$#
     2018-08-29 14:59:16.531755+0800 CouponPurchase[26186:6648795] string -- $z$
     2018-08-29 14:59:16.532189+0800 CouponPurchase[26186:6648795] result -- #$z$#
     2018-08-29 14:59:16.532799+0800 CouponPurchase[26186:6648795] string -- $A$
     2018-08-29 14:59:16.533285+0800 CouponPurchase[26186:6648795] result -- #$A$#
     2018-08-29 14:59:16.533677+0800 CouponPurchase[26186:6648795] string -- $B$
     2018-08-29 14:59:16.534035+0800 CouponPurchase[26186:6648795] result -- #$B$#
     2018-08-29 14:59:16.534655+0800 CouponPurchase[26186:6648795] string -- $C$
     2018-08-29 14:59:16.534923+0800 CouponPurchase[26186:6648795] result -- #$C$#
     2018-08-29 14:59:16.535163+0800 CouponPurchase[26186:6648795] string -- $D$
     2018-08-29 14:59:16.535300+0800 CouponPurchase[26186:6648795] result -- #$D$#
     2018-08-29 14:59:16.535625+0800 CouponPurchase[26186:6648795] string -- $E$
     2018-08-29 14:59:16.536159+0800 CouponPurchase[26186:6648795] result -- #$E$#
     2018-08-29 14:59:16.536622+0800 CouponPurchase[26186:6648795] string -- $F$
     2018-08-29 14:59:16.537127+0800 CouponPurchase[26186:6648795] result -- #$F$#
     2018-08-29 14:59:16.538461+0800 CouponPurchase[26186:6648795] string -- $G$
     2018-08-29 14:59:16.539773+0800 CouponPurchase[26186:6648795] result -- #$G$#
     2018-08-29 14:59:16.540327+0800 CouponPurchase[26186:6648795] string -- $H$
     2018-08-29 14:59:16.540745+0800 CouponPurchase[26186:6648795] result -- #$H$#
     2018-08-29 14:59:16.541273+0800 CouponPurchase[26186:6648795] string -- $I$
     2018-08-29 14:59:16.541581+0800 CouponPurchase[26186:6648795] result -- #$I$#
     2018-08-29 14:59:16.541828+0800 CouponPurchase[26186:6648795] string -- $J$
     2018-08-29 14:59:16.542076+0800 CouponPurchase[26186:6648795] result -- #$J$#
     2018-08-29 14:59:16.542342+0800 CouponPurchase[26186:6648795] string -- $K$
     2018-08-29 14:59:16.542777+0800 CouponPurchase[26186:6648795] result -- #$K$#
     2018-08-29 14:59:16.543038+0800 CouponPurchase[26186:6648795] string -- $L$
     2018-08-29 14:59:16.544114+0800 CouponPurchase[26186:6648795] result -- #$L$#
     2018-08-29 14:59:16.544524+0800 CouponPurchase[26186:6648795] string -- $M$
     2018-08-29 14:59:16.544777+0800 CouponPurchase[26186:6648795] result -- #$M$#
     2018-08-29 14:59:16.545016+0800 CouponPurchase[26186:6648795] string -- $N$
     2018-08-29 14:59:16.545255+0800 CouponPurchase[26186:6648795] result -- #$N$#
     2018-08-29 14:59:16.545479+0800 CouponPurchase[26186:6648795] string -- $O$
     2018-08-29 14:59:16.545700+0800 CouponPurchase[26186:6648795] result -- #$O$#
     2018-08-29 14:59:16.545935+0800 CouponPurchase[26186:6648795] string -- $P$
     2018-08-29 14:59:16.546236+0800 CouponPurchase[26186:6648795] result -- #$P$#
     2018-08-29 14:59:16.546482+0800 CouponPurchase[26186:6648795] string -- $Q$
     2018-08-29 14:59:16.546676+0800 CouponPurchase[26186:6648795] result -- #$Q$#
     2018-08-29 14:59:16.547085+0800 CouponPurchase[26186:6648795] string -- $R$
     2018-08-29 14:59:16.547425+0800 CouponPurchase[26186:6648795] result -- #$R$#
     2018-08-29 14:59:16.547785+0800 CouponPurchase[26186:6648795] string -- $S$
     2018-08-29 14:59:16.547987+0800 CouponPurchase[26186:6648795] result -- #$S$#
     2018-08-29 14:59:16.550780+0800 CouponPurchase[26186:6648795] string -- $T$
     2018-08-29 14:59:16.550943+0800 CouponPurchase[26186:6648795] result -- #$T$#
     2018-08-29 14:59:16.551081+0800 CouponPurchase[26186:6648795] string -- $U$
     2018-08-29 14:59:16.551232+0800 CouponPurchase[26186:6648795] result -- #$U$#
     2018-08-29 14:59:16.551348+0800 CouponPurchase[26186:6648795] string -- $V$
     2018-08-29 14:59:16.551467+0800 CouponPurchase[26186:6648795] result -- #$V$#
     2018-08-29 14:59:16.551582+0800 CouponPurchase[26186:6648795] string -- $W$
     2018-08-29 14:59:16.551699+0800 CouponPurchase[26186:6648795] result -- #$W$#
     2018-08-29 14:59:16.551810+0800 CouponPurchase[26186:6648795] string -- $X$
     2018-08-29 14:59:16.551916+0800 CouponPurchase[26186:6648795] result -- #$X$#
     2018-08-29 14:59:16.552027+0800 CouponPurchase[26186:6648795] string -- $Y$
     2018-08-29 14:59:16.552143+0800 CouponPurchase[26186:6648795] result -- #$Y$#
     2018-08-29 14:59:16.552413+0800 CouponPurchase[26186:6648795] string -- $Z$
     2018-08-29 14:59:16.552730+0800 CouponPurchase[26186:6648795] result -- #$Z$#
     */
}

但是当使用NSMutableString进行如上操作时,就出现了问题。

测试用例:

- (void)testNSMutableString
{
    for (int i = 0; i < 10; i++) {
        NSString *string = [[@"$" stringByAppendingString:[@(i) stringValue]] stringByAppendingString:@"$"];
        NSLog(@"string -- %@", string);
        NSMutableString *result = [NSMutableString stringWithString:@"#测试#"];
        [result replaceOccurrencesOfString:@"测试" withString:string options:NSRegularExpressionSearch range:NSMakeRange(0, result.length)];
        NSLog(@"result -- %@", result);
    }

    NSArray *array = @[
                       @"a",
                       @"b",
                       @"c",
                       @"d",
                       @"e",
                       @"f",
                       @"g",
                       @"h",
                       @"i",
                       @"j",
                       @"k",
                       @"l",
                       @"m",
                       @"n",
                       @"o",
                       @"p",
                       @"q",
                       @"r",
                       @"s",
                       @"t",
                       @"u",
                       @"v",
                       @"w",
                       @"x",
                       @"y",
                       @"z",
                       ];

    for (NSString *s in array) {
        NSString *string = [[@"$" stringByAppendingString:s] stringByAppendingString:@"$"];
        NSLog(@"string -- %@", string);
        NSMutableString *result = [NSMutableString stringWithString:@"#测试#"];
        [result replaceOccurrencesOfString:@"测试" withString:string options:NSRegularExpressionSearch range:NSMakeRange(0, result.length)];
        NSLog(@"result -- %@", result);
    }

    NSArray *ARRAY = @[
                       @"A",
                       @"B",
                       @"C",
                       @"D",
                       @"E",
                       @"F",
                       @"G",
                       @"H",
                       @"I",
                       @"J",
                       @"K",
                       @"L",
                       @"M",
                       @"N",
                       @"O",
                       @"P",
                       @"Q",
                       @"R",
                       @"S",
                       @"T",
                       @"U",
                       @"V",
                       @"W",
                       @"X",
                       @"Y",
                       @"Z",
                       ];

    for (NSString *s in ARRAY) {
        NSString *string = [[@"$" stringByAppendingString:s] stringByAppendingString:@"$"];
        NSLog(@"string -- %@", string);
        NSMutableString *result = [NSMutableString stringWithString:@"#测试#"];
        [result replaceOccurrencesOfString:@"测试" withString:string options:NSRegularExpressionSearch range:NSMakeRange(0, result.length)];
        NSLog(@"result -- %@", result);
    }

    // 打印日志:
    /*
     2018-08-29 15:07:25.829195+0800 CouponPurchase[27367:6676309] string -- $0$
     2018-08-29 15:07:25.835894+0800 CouponPurchase[27367:6676309] result -- #测试$#
     2018-08-29 15:07:25.836246+0800 CouponPurchase[27367:6676309] string -- $1$
     2018-08-29 15:07:25.836896+0800 CouponPurchase[27367:6676309] result -- #$#
     2018-08-29 15:07:25.837033+0800 CouponPurchase[27367:6676309] string -- $2$
     2018-08-29 15:07:25.837475+0800 CouponPurchase[27367:6676309] result -- #$#
     2018-08-29 15:07:25.837817+0800 CouponPurchase[27367:6676309] string -- $3$
     2018-08-29 15:07:25.838298+0800 CouponPurchase[27367:6676309] result -- #$#
     2018-08-29 15:07:25.840156+0800 CouponPurchase[27367:6676309] string -- $4$
     2018-08-29 15:07:25.840528+0800 CouponPurchase[27367:6676309] result -- #$#
     2018-08-29 15:07:25.840865+0800 CouponPurchase[27367:6676309] string -- $5$
     2018-08-29 15:07:25.841466+0800 CouponPurchase[27367:6676309] result -- #$#
     2018-08-29 15:07:25.841793+0800 CouponPurchase[27367:6676309] string -- $6$
     2018-08-29 15:07:25.849680+0800 CouponPurchase[27367:6676309] result -- #$#
     2018-08-29 15:07:25.849951+0800 CouponPurchase[27367:6676309] string -- $7$
     2018-08-29 15:07:25.850769+0800 CouponPurchase[27367:6676309] result -- #$#
     2018-08-29 15:07:25.851303+0800 CouponPurchase[27367:6676309] string -- $8$
     2018-08-29 15:07:25.851528+0800 CouponPurchase[27367:6676309] result -- #$#
     2018-08-29 15:07:25.851682+0800 CouponPurchase[27367:6676309] string -- $9$
     2018-08-29 15:07:25.853119+0800 CouponPurchase[27367:6676309] result -- #$#
     2018-08-29 15:07:25.853446+0800 CouponPurchase[27367:6676309] string -- $a$
     2018-08-29 15:07:25.853632+0800 CouponPurchase[27367:6676309] result -- #$a$#
     2018-08-29 15:07:25.853743+0800 CouponPurchase[27367:6676309] string -- $b$
     2018-08-29 15:07:25.853974+0800 CouponPurchase[27367:6676309] result -- #$b$#
     2018-08-29 15:07:25.854103+0800 CouponPurchase[27367:6676309] string -- $c$
     2018-08-29 15:07:25.854589+0800 CouponPurchase[27367:6676309] result -- #$c$#
     2018-08-29 15:07:25.854939+0800 CouponPurchase[27367:6676309] string -- $d$
     2018-08-29 15:07:25.855297+0800 CouponPurchase[27367:6676309] result -- #$d$#
     2018-08-29 15:07:25.855521+0800 CouponPurchase[27367:6676309] string -- $e$
     2018-08-29 15:07:25.870574+0800 CouponPurchase[27367:6676309] result -- #$e$#
     2018-08-29 15:07:25.871415+0800 CouponPurchase[27367:6676309] string -- $f$
     2018-08-29 15:07:25.872773+0800 CouponPurchase[27367:6676309] result -- #$f$#
     2018-08-29 15:07:25.873055+0800 CouponPurchase[27367:6676309] string -- $g$
     2018-08-29 15:07:25.873332+0800 CouponPurchase[27367:6676309] result -- #$g$#
     2018-08-29 15:07:25.874353+0800 CouponPurchase[27367:6676309] string -- $h$
     2018-08-29 15:07:25.875009+0800 CouponPurchase[27367:6676309] result -- #$h$#
     2018-08-29 15:07:25.875764+0800 CouponPurchase[27367:6676309] string -- $i$
     2018-08-29 15:07:25.876453+0800 CouponPurchase[27367:6676309] result -- #$i$#
     2018-08-29 15:07:25.877002+0800 CouponPurchase[27367:6676309] string -- $j$
     2018-08-29 15:07:25.877656+0800 CouponPurchase[27367:6676309] result -- #$j$#
     2018-08-29 15:07:25.878658+0800 CouponPurchase[27367:6676309] string -- $k$
     2018-08-29 15:07:25.878843+0800 CouponPurchase[27367:6676309] result -- #$k$#
     2018-08-29 15:07:25.878975+0800 CouponPurchase[27367:6676309] string -- $l$
     2018-08-29 15:07:25.879172+0800 CouponPurchase[27367:6676309] result -- #$l$#
     2018-08-29 15:07:25.879352+0800 CouponPurchase[27367:6676309] string -- $m$
     2018-08-29 15:07:25.880821+0800 CouponPurchase[27367:6676309] result -- #$m$#
     2018-08-29 15:07:25.881344+0800 CouponPurchase[27367:6676309] string -- $n$
     2018-08-29 15:07:25.881579+0800 CouponPurchase[27367:6676309] result -- #$n$#
     2018-08-29 15:07:25.881775+0800 CouponPurchase[27367:6676309] string -- $o$
     2018-08-29 15:07:25.882077+0800 CouponPurchase[27367:6676309] result -- #$o$#
     2018-08-29 15:07:25.882289+0800 CouponPurchase[27367:6676309] string -- $p$
     2018-08-29 15:07:25.882436+0800 CouponPurchase[27367:6676309] result -- #$p$#
     2018-08-29 15:07:25.883687+0800 CouponPurchase[27367:6676309] string -- $q$
     2018-08-29 15:07:25.883934+0800 CouponPurchase[27367:6676309] result -- #$q$#
     2018-08-29 15:07:25.884155+0800 CouponPurchase[27367:6676309] string -- $r$
     2018-08-29 15:07:25.884576+0800 CouponPurchase[27367:6676309] result -- #$r$#
     2018-08-29 15:07:25.884704+0800 CouponPurchase[27367:6676309] string -- $s$
     2018-08-29 15:07:25.884867+0800 CouponPurchase[27367:6676309] result -- #$s$#
     2018-08-29 15:07:25.885001+0800 CouponPurchase[27367:6676309] string -- $t$
     2018-08-29 15:07:25.885193+0800 CouponPurchase[27367:6676309] result -- #$t$#
     2018-08-29 15:07:25.885396+0800 CouponPurchase[27367:6676309] string -- $u$
     2018-08-29 15:07:25.885646+0800 CouponPurchase[27367:6676309] result -- #$u$#
     2018-08-29 15:07:25.885807+0800 CouponPurchase[27367:6676309] string -- $v$
     2018-08-29 15:07:25.886079+0800 CouponPurchase[27367:6676309] result -- #$v$#
     2018-08-29 15:07:25.886218+0800 CouponPurchase[27367:6676309] string -- $w$
     2018-08-29 15:07:25.886512+0800 CouponPurchase[27367:6676309] result -- #$w$#
     2018-08-29 15:07:25.886653+0800 CouponPurchase[27367:6676309] string -- $x$
     2018-08-29 15:07:25.886834+0800 CouponPurchase[27367:6676309] result -- #$x$#
     2018-08-29 15:07:25.887007+0800 CouponPurchase[27367:6676309] string -- $y$
     2018-08-29 15:07:25.887203+0800 CouponPurchase[27367:6676309] result -- #$y$#
     2018-08-29 15:07:25.887418+0800 CouponPurchase[27367:6676309] string -- $z$
     2018-08-29 15:07:25.887853+0800 CouponPurchase[27367:6676309] result -- #$z$#
     2018-08-29 15:07:25.888299+0800 CouponPurchase[27367:6676309] string -- $A$
     2018-08-29 15:07:25.888723+0800 CouponPurchase[27367:6676309] result -- #$A$#
     2018-08-29 15:07:25.889882+0800 CouponPurchase[27367:6676309] string -- $B$
     2018-08-29 15:07:25.890097+0800 CouponPurchase[27367:6676309] result -- #$B$#
     2018-08-29 15:07:25.890408+0800 CouponPurchase[27367:6676309] string -- $C$
     2018-08-29 15:07:25.891103+0800 CouponPurchase[27367:6676309] result -- #$C$#
     2018-08-29 15:07:25.891437+0800 CouponPurchase[27367:6676309] string -- $D$
     2018-08-29 15:07:25.892704+0800 CouponPurchase[27367:6676309] result -- #$D$#
     2018-08-29 15:07:25.893195+0800 CouponPurchase[27367:6676309] string -- $E$
     2018-08-29 15:07:25.893764+0800 CouponPurchase[27367:6676309] result -- #$E$#
     2018-08-29 15:07:25.893934+0800 CouponPurchase[27367:6676309] string -- $F$
     2018-08-29 15:07:25.894246+0800 CouponPurchase[27367:6676309] result -- #$F$#
     2018-08-29 15:07:25.894749+0800 CouponPurchase[27367:6676309] string -- $G$
     2018-08-29 15:07:25.896132+0800 CouponPurchase[27367:6676309] result -- #$G$#
     2018-08-29 15:07:25.897263+0800 CouponPurchase[27367:6676309] string -- $H$
     2018-08-29 15:07:25.898201+0800 CouponPurchase[27367:6676309] result -- #$H$#
     2018-08-29 15:07:25.898392+0800 CouponPurchase[27367:6676309] string -- $I$
     2018-08-29 15:07:25.898645+0800 CouponPurchase[27367:6676309] result -- #$I$#
     2018-08-29 15:07:25.898989+0800 CouponPurchase[27367:6676309] string -- $J$
     2018-08-29 15:07:25.899422+0800 CouponPurchase[27367:6676309] result -- #$J$#
     2018-08-29 15:07:25.899655+0800 CouponPurchase[27367:6676309] string -- $K$
     2018-08-29 15:07:25.899924+0800 CouponPurchase[27367:6676309] result -- #$K$#
     2018-08-29 15:07:25.900254+0800 CouponPurchase[27367:6676309] string -- $L$
     2018-08-29 15:07:25.900636+0800 CouponPurchase[27367:6676309] result -- #$L$#
     2018-08-29 15:07:25.900948+0800 CouponPurchase[27367:6676309] string -- $M$
     2018-08-29 15:07:25.902058+0800 CouponPurchase[27367:6676309] result -- #$M$#
     2018-08-29 15:07:25.902240+0800 CouponPurchase[27367:6676309] string -- $N$
     2018-08-29 15:07:25.902495+0800 CouponPurchase[27367:6676309] result -- #$N$#
     2018-08-29 15:07:25.902636+0800 CouponPurchase[27367:6676309] string -- $O$
     2018-08-29 15:07:25.903056+0800 CouponPurchase[27367:6676309] result -- #$O$#
     2018-08-29 15:07:25.904029+0800 CouponPurchase[27367:6676309] string -- $P$
     2018-08-29 15:07:25.904698+0800 CouponPurchase[27367:6676309] result -- #$P$#
     2018-08-29 15:07:25.905614+0800 CouponPurchase[27367:6676309] string -- $Q$
     2018-08-29 15:07:25.905839+0800 CouponPurchase[27367:6676309] result -- #$Q$#
     2018-08-29 15:07:25.905999+0800 CouponPurchase[27367:6676309] string -- $R$
     2018-08-29 15:07:25.906196+0800 CouponPurchase[27367:6676309] result -- #$R$#
     2018-08-29 15:07:25.906365+0800 CouponPurchase[27367:6676309] string -- $S$
     2018-08-29 15:07:25.906699+0800 CouponPurchase[27367:6676309] result -- #$S$#
     2018-08-29 15:07:25.908239+0800 CouponPurchase[27367:6676309] string -- $T$
     2018-08-29 15:07:25.909489+0800 CouponPurchase[27367:6676309] result -- #$T$#
     2018-08-29 15:07:25.909782+0800 CouponPurchase[27367:6676309] string -- $U$
     2018-08-29 15:07:25.910739+0800 CouponPurchase[27367:6676309] result -- #$U$#
     2018-08-29 15:07:25.910943+0800 CouponPurchase[27367:6676309] string -- $V$
     2018-08-29 15:07:25.911402+0800 CouponPurchase[27367:6676309] result -- #$V$#
     2018-08-29 15:07:25.911586+0800 CouponPurchase[27367:6676309] string -- $W$
     2018-08-29 15:07:25.911903+0800 CouponPurchase[27367:6676309] result -- #$W$#
     2018-08-29 15:07:25.912444+0800 CouponPurchase[27367:6676309] string -- $X$
     2018-08-29 15:07:25.913056+0800 CouponPurchase[27367:6676309] result -- #$X$#
     2018-08-29 15:07:25.914131+0800 CouponPurchase[27367:6676309] string -- $Y$
     2018-08-29 15:07:25.914743+0800 CouponPurchase[27367:6676309] result -- #$Y$#
     2018-08-29 15:07:25.915296+0800 CouponPurchase[27367:6676309] string -- $Z$
     2018-08-29 15:07:25.916083+0800 CouponPurchase[27367:6676309] result -- #$Z$#
     */
}

WTF, $ 后面紧跟数字的那些怎么会出现替换问题?为什么?其实在上面的替换方法中匹配选项设置为NSRegularExpressionSearch,而 $ 恰好在正则表达式中有特殊含义,所以,猜测是这个原因。

那么,该如何解决呢?其实解决方法很简单(找了两个小时的方法)。

代码如下:

- (void)testCollectNSMutableString
{
    for (int i = 0; i < 10; i++) {
        NSString *string = [[@"$" stringByAppendingString:[@(i) stringValue]] stringByAppendingString:@"$"];
        NSLog(@"string -- %@", string);
        NSMutableString *result = [NSMutableString stringWithString:@"#测试#"];
        string = [@"\\" stringByAppendingString:string];
        [result replaceOccurrencesOfString:@"测试" withString:string options:NSRegularExpressionSearch range:NSMakeRange(0, result.length)];
        NSLog(@"result -- %@", result);
    }

    NSArray *array = @[
                       @"a",
                       @"b",
                       @"c",
                       @"d",
                       @"e",
                       @"f",
                       @"g",
                       @"h",
                       @"i",
                       @"j",
                       @"k",
                       @"l",
                       @"m",
                       @"n",
                       @"o",
                       @"p",
                       @"q",
                       @"r",
                       @"s",
                       @"t",
                       @"u",
                       @"v",
                       @"w",
                       @"x",
                       @"y",
                       @"z",
                       ];

    for (NSString *s in array) {
        NSString *string = [[@"$" stringByAppendingString:s] stringByAppendingString:@"$"];
        NSLog(@"string -- %@", string);
        NSMutableString *result = [NSMutableString stringWithString:@"#测试#"];
        string = [@"\\" stringByAppendingString:string];
        [result replaceOccurrencesOfString:@"测试" withString:string options:NSRegularExpressionSearch range:NSMakeRange(0, result.length)];
        NSLog(@"result -- %@", result);
    }

    NSArray *ARRAY = @[
                       @"A",
                       @"B",
                       @"C",
                       @"D",
                       @"E",
                       @"F",
                       @"G",
                       @"H",
                       @"I",
                       @"J",
                       @"K",
                       @"L",
                       @"M",
                       @"N",
                       @"O",
                       @"P",
                       @"Q",
                       @"R",
                       @"S",
                       @"T",
                       @"U",
                       @"V",
                       @"W",
                       @"X",
                       @"Y",
                       @"Z",
                       ];

    for (NSString *s in ARRAY) {
        NSString *string = [[@"$" stringByAppendingString:s] stringByAppendingString:@"$"];
        NSLog(@"string -- %@", string);
        NSMutableString *result = [NSMutableString stringWithString:@"#测试#"];
        string = [@"\\" stringByAppendingString:string];
        [result replaceOccurrencesOfString:@"测试" withString:string options:NSRegularExpressionSearch range:NSMakeRange(0, result.length)];
        NSLog(@"result -- %@", result);
    }

    // 打印日志:
    /*
     2018-08-29 16:02:31.584032+0800 CouponPurchase[43294:6890002] string -- $0$
     2018-08-29 16:02:31.585522+0800 CouponPurchase[43294:6890002] result -- #$0$#
     2018-08-29 16:02:31.585795+0800 CouponPurchase[43294:6890002] string -- $1$
     2018-08-29 16:02:31.586149+0800 CouponPurchase[43294:6890002] result -- #$1$#
     2018-08-29 16:02:31.586450+0800 CouponPurchase[43294:6890002] string -- $2$
     2018-08-29 16:02:31.586745+0800 CouponPurchase[43294:6890002] result -- #$2$#
     2018-08-29 16:02:31.586991+0800 CouponPurchase[43294:6890002] string -- $3$
     2018-08-29 16:02:31.587291+0800 CouponPurchase[43294:6890002] result -- #$3$#
     2018-08-29 16:02:31.587434+0800 CouponPurchase[43294:6890002] string -- $4$
     2018-08-29 16:02:31.587691+0800 CouponPurchase[43294:6890002] result -- #$4$#
     2018-08-29 16:02:31.587991+0800 CouponPurchase[43294:6890002] string -- $5$
     2018-08-29 16:02:31.588822+0800 CouponPurchase[43294:6890002] result -- #$5$#
     2018-08-29 16:02:31.589203+0800 CouponPurchase[43294:6890002] string -- $6$
     2018-08-29 16:02:31.589545+0800 CouponPurchase[43294:6890002] result -- #$6$#
     2018-08-29 16:02:31.610387+0800 CouponPurchase[43294:6890002] string -- $7$
     2018-08-29 16:02:31.610701+0800 CouponPurchase[43294:6890002] result -- #$7$#
     2018-08-29 16:02:31.610825+0800 CouponPurchase[43294:6890002] string -- $8$
     2018-08-29 16:02:31.610980+0800 CouponPurchase[43294:6890002] result -- #$8$#
     2018-08-29 16:02:31.611097+0800 CouponPurchase[43294:6890002] string -- $9$
     2018-08-29 16:02:31.611229+0800 CouponPurchase[43294:6890002] result -- #$9$#
     2018-08-29 16:02:31.611377+0800 CouponPurchase[43294:6890002] string -- $a$
     2018-08-29 16:02:31.611650+0800 CouponPurchase[43294:6890002] result -- #$a$#
     2018-08-29 16:02:31.612852+0800 CouponPurchase[43294:6890002] string -- $b$
     2018-08-29 16:02:31.613397+0800 CouponPurchase[43294:6890002] result -- #$b$#
     2018-08-29 16:02:31.614130+0800 CouponPurchase[43294:6890002] string -- $c$
     2018-08-29 16:02:31.614544+0800 CouponPurchase[43294:6890002] result -- #$c$#
     2018-08-29 16:02:31.614713+0800 CouponPurchase[43294:6890002] string -- $d$
     2018-08-29 16:02:31.615336+0800 CouponPurchase[43294:6890002] result -- #$d$#
     2018-08-29 16:02:31.615950+0800 CouponPurchase[43294:6890002] string -- $e$
     2018-08-29 16:02:31.616353+0800 CouponPurchase[43294:6890002] result -- #$e$#
     2018-08-29 16:02:31.616957+0800 CouponPurchase[43294:6890002] string -- $f$
     2018-08-29 16:02:31.630439+0800 CouponPurchase[43294:6890002] result -- #$f$#
     2018-08-29 16:02:31.630792+0800 CouponPurchase[43294:6890002] string -- $g$
     2018-08-29 16:02:31.631443+0800 CouponPurchase[43294:6890002] result -- #$g$#
     2018-08-29 16:02:31.632003+0800 CouponPurchase[43294:6890002] string -- $h$
     2018-08-29 16:02:31.632152+0800 CouponPurchase[43294:6890002] result -- #$h$#
     2018-08-29 16:02:31.632785+0800 CouponPurchase[43294:6890002] string -- $i$
     2018-08-29 16:02:31.632952+0800 CouponPurchase[43294:6890002] result -- #$i$#
     2018-08-29 16:02:31.633123+0800 CouponPurchase[43294:6890002] string -- $j$
     2018-08-29 16:02:31.636711+0800 CouponPurchase[43294:6890002] result -- #$j$#
     2018-08-29 16:02:31.643582+0800 CouponPurchase[43294:6890002] string -- $k$
     2018-08-29 16:02:31.645128+0800 CouponPurchase[43294:6890002] result -- #$k$#
     2018-08-29 16:02:31.645890+0800 CouponPurchase[43294:6890002] string -- $l$
     2018-08-29 16:02:31.647152+0800 CouponPurchase[43294:6890002] result -- #$l$#
     2018-08-29 16:02:31.648501+0800 CouponPurchase[43294:6890002] string -- $m$
     2018-08-29 16:02:31.651267+0800 CouponPurchase[43294:6890002] result -- #$m$#
     2018-08-29 16:02:31.651908+0800 CouponPurchase[43294:6890002] string -- $n$
     2018-08-29 16:02:31.653358+0800 CouponPurchase[43294:6890002] result -- #$n$#
     2018-08-29 16:02:31.654566+0800 CouponPurchase[43294:6890002] string -- $o$
     2018-08-29 16:02:31.655148+0800 CouponPurchase[43294:6890002] result -- #$o$#
     2018-08-29 16:02:31.655725+0800 CouponPurchase[43294:6890002] string -- $p$
     2018-08-29 16:02:31.656089+0800 CouponPurchase[43294:6890002] result -- #$p$#
     2018-08-29 16:02:31.657208+0800 CouponPurchase[43294:6890002] string -- $q$
     2018-08-29 16:02:31.657731+0800 CouponPurchase[43294:6890002] result -- #$q$#
     2018-08-29 16:02:31.658761+0800 CouponPurchase[43294:6890002] string -- $r$
     2018-08-29 16:02:31.659334+0800 CouponPurchase[43294:6890002] result -- #$r$#
     2018-08-29 16:02:31.659794+0800 CouponPurchase[43294:6890002] string -- $s$
     2018-08-29 16:02:31.660731+0800 CouponPurchase[43294:6890002] result -- #$s$#
     2018-08-29 16:02:31.661253+0800 CouponPurchase[43294:6890002] string -- $t$
     2018-08-29 16:02:31.662192+0800 CouponPurchase[43294:6890002] result -- #$t$#
     2018-08-29 16:02:31.663213+0800 CouponPurchase[43294:6890002] string -- $u$
     2018-08-29 16:02:31.663800+0800 CouponPurchase[43294:6890002] result -- #$u$#
     2018-08-29 16:02:31.664708+0800 CouponPurchase[43294:6890002] string -- $v$
     2018-08-29 16:02:31.666214+0800 CouponPurchase[43294:6890002] result -- #$v$#
     2018-08-29 16:02:31.666509+0800 CouponPurchase[43294:6890002] string -- $w$
     2018-08-29 16:02:31.669117+0800 CouponPurchase[43294:6890002] result -- #$w$#
     2018-08-29 16:02:31.670544+0800 CouponPurchase[43294:6890002] string -- $x$
     2018-08-29 16:02:31.671238+0800 CouponPurchase[43294:6890002] result -- #$x$#
     2018-08-29 16:02:31.673670+0800 CouponPurchase[43294:6890002] string -- $y$
     2018-08-29 16:02:31.673994+0800 CouponPurchase[43294:6890002] result -- #$y$#
     2018-08-29 16:02:31.674388+0800 CouponPurchase[43294:6890002] string -- $z$
     2018-08-29 16:02:31.675564+0800 CouponPurchase[43294:6890002] result -- #$z$#
     2018-08-29 16:02:31.676306+0800 CouponPurchase[43294:6890002] string -- $A$
     2018-08-29 16:02:31.679461+0800 CouponPurchase[43294:6890002] result -- #$A$#
     2018-08-29 16:02:31.679685+0800 CouponPurchase[43294:6890002] string -- $B$
     2018-08-29 16:02:31.680024+0800 CouponPurchase[43294:6890002] result -- #$B$#
     2018-08-29 16:02:31.680842+0800 CouponPurchase[43294:6890002] string -- $C$
     2018-08-29 16:02:31.682236+0800 CouponPurchase[43294:6890002] result -- #$C$#
     2018-08-29 16:02:31.683795+0800 CouponPurchase[43294:6890002] string -- $D$
     2018-08-29 16:02:31.684499+0800 CouponPurchase[43294:6890002] result -- #$D$#
     2018-08-29 16:02:31.685106+0800 CouponPurchase[43294:6890002] string -- $E$
     2018-08-29 16:02:31.685310+0800 CouponPurchase[43294:6890002] result -- #$E$#
     2018-08-29 16:02:31.686215+0800 CouponPurchase[43294:6890002] string -- $F$
     2018-08-29 16:02:31.686649+0800 CouponPurchase[43294:6890002] result -- #$F$#
     2018-08-29 16:02:31.687205+0800 CouponPurchase[43294:6890002] string -- $G$
     2018-08-29 16:02:31.687911+0800 CouponPurchase[43294:6890002] result -- #$G$#
     2018-08-29 16:02:31.688876+0800 CouponPurchase[43294:6890002] string -- $H$
     2018-08-29 16:02:31.689862+0800 CouponPurchase[43294:6890002] result -- #$H$#
     2018-08-29 16:02:31.690053+0800 CouponPurchase[43294:6890002] string -- $I$
     2018-08-29 16:02:31.691653+0800 CouponPurchase[43294:6890002] result -- #$I$#
     2018-08-29 16:02:31.691814+0800 CouponPurchase[43294:6890002] string -- $J$
     2018-08-29 16:02:31.692059+0800 CouponPurchase[43294:6890002] result -- #$J$#
     2018-08-29 16:02:31.692562+0800 CouponPurchase[43294:6890002] string -- $K$
     2018-08-29 16:02:31.692772+0800 CouponPurchase[43294:6890002] result -- #$K$#
     2018-08-29 16:02:31.693391+0800 CouponPurchase[43294:6890002] string -- $L$
     2018-08-29 16:02:31.694260+0800 CouponPurchase[43294:6890002] result -- #$L$#
     2018-08-29 16:02:31.695611+0800 CouponPurchase[43294:6890002] string -- $M$
     2018-08-29 16:02:31.696144+0800 CouponPurchase[43294:6890002] result -- #$M$#
     2018-08-29 16:02:31.696689+0800 CouponPurchase[43294:6890002] string -- $N$
     2018-08-29 16:02:31.697320+0800 CouponPurchase[43294:6890002] result -- #$N$#
     2018-08-29 16:02:31.697584+0800 CouponPurchase[43294:6890002] string -- $O$
     2018-08-29 16:02:31.698162+0800 CouponPurchase[43294:6890002] result -- #$O$#
     2018-08-29 16:02:31.700269+0800 CouponPurchase[43294:6890002] string -- $P$
     2018-08-29 16:02:31.700783+0800 CouponPurchase[43294:6890002] result -- #$P$#
     2018-08-29 16:02:31.701577+0800 CouponPurchase[43294:6890002] string -- $Q$
     2018-08-29 16:02:31.702555+0800 CouponPurchase[43294:6890002] result -- #$Q$#
     2018-08-29 16:02:31.702817+0800 CouponPurchase[43294:6890002] string -- $R$
     2018-08-29 16:02:31.703444+0800 CouponPurchase[43294:6890002] result -- #$R$#
     2018-08-29 16:02:31.703955+0800 CouponPurchase[43294:6890002] string -- $S$
     2018-08-29 16:02:31.704338+0800 CouponPurchase[43294:6890002] result -- #$S$#
     2018-08-29 16:02:31.704784+0800 CouponPurchase[43294:6890002] string -- $T$
     2018-08-29 16:02:31.705318+0800 CouponPurchase[43294:6890002] result -- #$T$#
     2018-08-29 16:02:31.705674+0800 CouponPurchase[43294:6890002] string -- $U$
     2018-08-29 16:02:31.706330+0800 CouponPurchase[43294:6890002] result -- #$U$#
     2018-08-29 16:02:31.707619+0800 CouponPurchase[43294:6890002] string -- $V$
     2018-08-29 16:02:31.708084+0800 CouponPurchase[43294:6890002] result -- #$V$#
     2018-08-29 16:02:31.709573+0800 CouponPurchase[43294:6890002] string -- $W$
     2018-08-29 16:02:31.709778+0800 CouponPurchase[43294:6890002] result -- #$W$#
     2018-08-29 16:02:31.710476+0800 CouponPurchase[43294:6890002] string -- $X$
     2018-08-29 16:02:31.710812+0800 CouponPurchase[43294:6890002] result -- #$X$#
     2018-08-29 16:02:31.711356+0800 CouponPurchase[43294:6890002] string -- $Y$
     2018-08-29 16:02:31.711934+0800 CouponPurchase[43294:6890002] result -- #$Y$#
     2018-08-29 16:02:31.712280+0800 CouponPurchase[43294:6890002] string -- $Z$
     2018-08-29 16:02:31.712698+0800 CouponPurchase[43294:6890002] result -- #$Z$#
     */
}

可以看到,只要在 $ 前面拼接 \\ ,替换结果就正常了。

有人可能说,你傻呀,换个方法不就行了嘛?

说的是没错,关键这TM是线上问题,你让我改个毛线呀!!!

下面换个方法进行替换。

- (void)testOther
{
    for (int i = 0; i < 10; i++) {
        NSString *string = [[@"$" stringByAppendingString:[@(i) stringValue]] stringByAppendingString:@"$"];
        NSLog(@"string -- %@", string);
        NSMutableString *result = [NSMutableString stringWithString:@"#测试#"];
        [result replaceOccurrencesOfString:@"测试" withString:string options:NSBackwardsSearch range:NSMakeRange(0, result.length)];
        NSLog(@"result -- %@", result);
    }

    NSArray *array = @[
                       @"a",
                       @"b",
                       @"c",
                       @"d",
                       @"e",
                       @"f",
                       @"g",
                       @"h",
                       @"i",
                       @"j",
                       @"k",
                       @"l",
                       @"m",
                       @"n",
                       @"o",
                       @"p",
                       @"q",
                       @"r",
                       @"s",
                       @"t",
                       @"u",
                       @"v",
                       @"w",
                       @"x",
                       @"y",
                       @"z",
                       ];

    for (NSString *s in array) {
        NSString *string = [[@"$" stringByAppendingString:s] stringByAppendingString:@"$"];
        NSLog(@"string -- %@", string);
        NSMutableString *result = [NSMutableString stringWithString:@"#测试#"];
        [result replaceOccurrencesOfString:@"测试" withString:string options:NSBackwardsSearch range:NSMakeRange(0, result.length)];
        NSLog(@"result -- %@", result);
    }

    NSArray *ARRAY = @[
                       @"A",
                       @"B",
                       @"C",
                       @"D",
                       @"E",
                       @"F",
                       @"G",
                       @"H",
                       @"I",
                       @"J",
                       @"K",
                       @"L",
                       @"M",
                       @"N",
                       @"O",
                       @"P",
                       @"Q",
                       @"R",
                       @"S",
                       @"T",
                       @"U",
                       @"V",
                       @"W",
                       @"X",
                       @"Y",
                       @"Z",
                       ];

    for (NSString *s in ARRAY) {
        NSString *string = [[@"$" stringByAppendingString:s] stringByAppendingString:@"$"];
        NSLog(@"string -- %@", string);
        NSMutableString *result = [NSMutableString stringWithString:@"#测试#"];
        [result replaceOccurrencesOfString:@"测试" withString:string options:NSBackwardsSearch range:NSMakeRange(0, result.length)];
        NSLog(@"result -- %@", result);
    }

    // 打印日志:
    /*
     2018-08-29 17:03:12.313213+0800 CouponPurchase[49091:7083143] string -- $0$
     2018-08-29 17:03:12.313381+0800 CouponPurchase[49091:7083143] result -- #$0$#
     2018-08-29 17:03:12.313540+0800 CouponPurchase[49091:7083143] string -- $1$
     2018-08-29 17:03:12.313722+0800 CouponPurchase[49091:7083143] result -- #$1$#
     2018-08-29 17:03:12.315021+0800 CouponPurchase[49091:7083143] string -- $2$
     2018-08-29 17:03:12.315137+0800 CouponPurchase[49091:7083143] result -- #$2$#
     2018-08-29 17:03:12.315247+0800 CouponPurchase[49091:7083143] string -- $3$
     2018-08-29 17:03:12.315346+0800 CouponPurchase[49091:7083143] result -- #$3$#
     2018-08-29 17:03:12.315480+0800 CouponPurchase[49091:7083143] string -- $4$
     2018-08-29 17:03:12.315592+0800 CouponPurchase[49091:7083143] result -- #$4$#
     2018-08-29 17:03:12.315702+0800 CouponPurchase[49091:7083143] string -- $5$
     2018-08-29 17:03:12.315852+0800 CouponPurchase[49091:7083143] result -- #$5$#
     2018-08-29 17:03:12.316229+0800 CouponPurchase[49091:7083143] string -- $6$
     2018-08-29 17:03:12.316498+0800 CouponPurchase[49091:7083143] result -- #$6$#
     2018-08-29 17:03:12.361263+0800 CouponPurchase[49091:7083143] string -- $7$
     2018-08-29 17:03:12.363544+0800 CouponPurchase[49091:7083143] result -- #$7$#
     2018-08-29 17:03:12.363996+0800 CouponPurchase[49091:7083143] string -- $8$
     2018-08-29 17:03:12.364137+0800 CouponPurchase[49091:7083143] result -- #$8$#
     2018-08-29 17:03:12.364268+0800 CouponPurchase[49091:7083143] string -- $9$
     2018-08-29 17:03:12.364359+0800 CouponPurchase[49091:7083143] result -- #$9$#
     2018-08-29 17:03:12.364467+0800 CouponPurchase[49091:7083143] string -- $a$
     2018-08-29 17:03:12.365001+0800 CouponPurchase[49091:7083143] result -- #$a$#
     2018-08-29 17:03:12.365637+0800 CouponPurchase[49091:7083143] string -- $b$
     2018-08-29 17:03:12.366259+0800 CouponPurchase[49091:7083143] result -- #$b$#
     2018-08-29 17:03:12.366423+0800 CouponPurchase[49091:7083143] string -- $c$
     2018-08-29 17:03:12.366536+0800 CouponPurchase[49091:7083143] result -- #$c$#
     2018-08-29 17:03:12.367452+0800 CouponPurchase[49091:7083143] string -- $d$
     2018-08-29 17:03:12.367831+0800 CouponPurchase[49091:7083143] result -- #$d$#
     2018-08-29 17:03:12.368146+0800 CouponPurchase[49091:7083143] string -- $e$
     2018-08-29 17:03:12.368469+0800 CouponPurchase[49091:7083143] result -- #$e$#
     2018-08-29 17:03:12.369060+0800 CouponPurchase[49091:7083143] string -- $f$
     2018-08-29 17:03:12.369300+0800 CouponPurchase[49091:7083143] result -- #$f$#
     2018-08-29 17:03:12.369394+0800 CouponPurchase[49091:7083143] string -- $g$
     2018-08-29 17:03:12.369535+0800 CouponPurchase[49091:7083143] result -- #$g$#
     2018-08-29 17:03:12.369631+0800 CouponPurchase[49091:7083143] string -- $h$
     2018-08-29 17:03:12.369824+0800 CouponPurchase[49091:7083143] result -- #$h$#
     2018-08-29 17:03:12.379477+0800 CouponPurchase[49091:7083143] string -- $i$
     2018-08-29 17:03:12.379744+0800 CouponPurchase[49091:7083143] result -- #$i$#
     2018-08-29 17:03:12.379859+0800 CouponPurchase[49091:7083143] string -- $j$
     2018-08-29 17:03:12.380031+0800 CouponPurchase[49091:7083143] result -- #$j$#
     2018-08-29 17:03:12.380213+0800 CouponPurchase[49091:7083143] string -- $k$
     2018-08-29 17:03:12.380304+0800 CouponPurchase[49091:7083143] result -- #$k$#
     2018-08-29 17:03:12.380405+0800 CouponPurchase[49091:7083143] string -- $l$
     2018-08-29 17:03:12.380491+0800 CouponPurchase[49091:7083143] result -- #$l$#
     2018-08-29 17:03:12.380571+0800 CouponPurchase[49091:7083143] string -- $m$
     2018-08-29 17:03:12.380646+0800 CouponPurchase[49091:7083143] result -- #$m$#
     2018-08-29 17:03:12.380721+0800 CouponPurchase[49091:7083143] string -- $n$
     2018-08-29 17:03:12.381287+0800 CouponPurchase[49091:7083143] result -- #$n$#
     2018-08-29 17:03:12.381593+0800 CouponPurchase[49091:7083143] string -- $o$
     2018-08-29 17:03:12.381762+0800 CouponPurchase[49091:7083143] result -- #$o$#
     2018-08-29 17:03:12.387808+0800 CouponPurchase[49091:7083143] string -- $p$
     2018-08-29 17:03:12.388068+0800 CouponPurchase[49091:7083143] result -- #$p$#
     2018-08-29 17:03:12.388180+0800 CouponPurchase[49091:7083143] string -- $q$
     2018-08-29 17:03:12.388279+0800 CouponPurchase[49091:7083143] result -- #$q$#
     2018-08-29 17:03:12.388376+0800 CouponPurchase[49091:7083143] string -- $r$
     2018-08-29 17:03:12.388534+0800 CouponPurchase[49091:7083143] result -- #$r$#
     2018-08-29 17:03:12.389749+0800 CouponPurchase[49091:7083143] string -- $s$
     2018-08-29 17:03:12.389910+0800 CouponPurchase[49091:7083143] result -- #$s$#
     2018-08-29 17:03:12.390247+0800 CouponPurchase[49091:7083143] string -- $t$
     2018-08-29 17:03:12.391745+0800 CouponPurchase[49091:7083143] result -- #$t$#
     2018-08-29 17:03:12.392703+0800 CouponPurchase[49091:7083143] string -- $u$
     2018-08-29 17:03:12.392810+0800 CouponPurchase[49091:7083143] result -- #$u$#
     2018-08-29 17:03:12.392904+0800 CouponPurchase[49091:7083143] string -- $v$
     2018-08-29 17:03:12.393053+0800 CouponPurchase[49091:7083143] result -- #$v$#
     2018-08-29 17:03:12.393152+0800 CouponPurchase[49091:7083143] string -- $w$
     2018-08-29 17:03:12.393248+0800 CouponPurchase[49091:7083143] result -- #$w$#
     2018-08-29 17:03:12.393351+0800 CouponPurchase[49091:7083143] string -- $x$
     2018-08-29 17:03:12.393751+0800 CouponPurchase[49091:7083143] result -- #$x$#
     2018-08-29 17:03:12.395266+0800 CouponPurchase[49091:7083143] string -- $y$
     2018-08-29 17:03:12.396103+0800 CouponPurchase[49091:7083143] result -- #$y$#
     2018-08-29 17:03:12.397581+0800 CouponPurchase[49091:7083143] string -- $z$
     2018-08-29 17:03:12.398740+0800 CouponPurchase[49091:7083143] result -- #$z$#
     2018-08-29 17:03:12.399026+0800 CouponPurchase[49091:7083143] string -- $A$
     2018-08-29 17:03:12.399224+0800 CouponPurchase[49091:7083143] result -- #$A$#
     2018-08-29 17:03:12.400932+0800 CouponPurchase[49091:7083143] string -- $B$
     2018-08-29 17:03:12.401159+0800 CouponPurchase[49091:7083143] result -- #$B$#
     2018-08-29 17:03:12.401264+0800 CouponPurchase[49091:7083143] string -- $C$
     2018-08-29 17:03:12.401391+0800 CouponPurchase[49091:7083143] result -- #$C$#
     2018-08-29 17:03:12.403625+0800 CouponPurchase[49091:7083143] string -- $D$
     2018-08-29 17:03:12.404611+0800 CouponPurchase[49091:7083143] result -- #$D$#
     2018-08-29 17:03:12.404987+0800 CouponPurchase[49091:7083143] string -- $E$
     2018-08-29 17:03:12.405725+0800 CouponPurchase[49091:7083143] result -- #$E$#
     2018-08-29 17:03:12.406590+0800 CouponPurchase[49091:7083143] string -- $F$
     2018-08-29 17:03:12.406883+0800 CouponPurchase[49091:7083143] result -- #$F$#
     2018-08-29 17:03:12.407457+0800 CouponPurchase[49091:7083143] string -- $G$
     2018-08-29 17:03:12.407773+0800 CouponPurchase[49091:7083143] result -- #$G$#
     2018-08-29 17:03:12.408321+0800 CouponPurchase[49091:7083143] string -- $H$
     2018-08-29 17:03:12.409204+0800 CouponPurchase[49091:7083143] result -- #$H$#
     2018-08-29 17:03:12.409330+0800 CouponPurchase[49091:7083143] string -- $I$
     2018-08-29 17:03:12.409425+0800 CouponPurchase[49091:7083143] result -- #$I$#
     2018-08-29 17:03:12.409533+0800 CouponPurchase[49091:7083143] string -- $J$
     2018-08-29 17:03:12.409722+0800 CouponPurchase[49091:7083143] result -- #$J$#
     2018-08-29 17:03:12.409897+0800 CouponPurchase[49091:7083143] string -- $K$
     2018-08-29 17:03:12.411373+0800 CouponPurchase[49091:7083143] result -- #$K$#
     2018-08-29 17:03:12.411767+0800 CouponPurchase[49091:7083143] string -- $L$
     2018-08-29 17:03:12.412338+0800 CouponPurchase[49091:7083143] result -- #$L$#
     2018-08-29 17:03:12.412909+0800 CouponPurchase[49091:7083143] string -- $M$
     2018-08-29 17:03:12.413233+0800 CouponPurchase[49091:7083143] result -- #$M$#
     2018-08-29 17:03:12.413440+0800 CouponPurchase[49091:7083143] string -- $N$
     2018-08-29 17:03:12.413667+0800 CouponPurchase[49091:7083143] result -- #$N$#
     2018-08-29 17:03:12.414512+0800 CouponPurchase[49091:7083143] string -- $O$
     2018-08-29 17:03:12.414922+0800 CouponPurchase[49091:7083143] result -- #$O$#
     2018-08-29 17:03:12.415063+0800 CouponPurchase[49091:7083143] string -- $P$
     2018-08-29 17:03:12.415213+0800 CouponPurchase[49091:7083143] result -- #$P$#
     2018-08-29 17:03:12.415378+0800 CouponPurchase[49091:7083143] string -- $Q$
     2018-08-29 17:03:12.416079+0800 CouponPurchase[49091:7083143] result -- #$Q$#
     2018-08-29 17:03:12.416835+0800 CouponPurchase[49091:7083143] string -- $R$
     2018-08-29 17:03:12.417612+0800 CouponPurchase[49091:7083143] result -- #$R$#
     2018-08-29 17:03:12.418413+0800 CouponPurchase[49091:7083143] string -- $S$
     2018-08-29 17:03:12.419044+0800 CouponPurchase[49091:7083143] result -- #$S$#
     2018-08-29 17:03:12.420037+0800 CouponPurchase[49091:7083143] string -- $T$
     2018-08-29 17:03:12.421684+0800 CouponPurchase[49091:7083143] result -- #$T$#
     2018-08-29 17:03:12.422761+0800 CouponPurchase[49091:7083143] string -- $U$
     2018-08-29 17:03:12.422886+0800 CouponPurchase[49091:7083143] result -- #$U$#
     2018-08-29 17:03:12.423206+0800 CouponPurchase[49091:7083143] string -- $V$
     2018-08-29 17:03:12.423436+0800 CouponPurchase[49091:7083143] result -- #$V$#
     2018-08-29 17:03:12.423780+0800 CouponPurchase[49091:7083143] string -- $W$
     2018-08-29 17:03:12.423904+0800 CouponPurchase[49091:7083143] result -- #$W$#
     2018-08-29 17:03:12.424008+0800 CouponPurchase[49091:7083143] string -- $X$
     2018-08-29 17:03:12.424105+0800 CouponPurchase[49091:7083143] result -- #$X$#
     2018-08-29 17:03:12.424237+0800 CouponPurchase[49091:7083143] string -- $Y$
     2018-08-29 17:03:12.424364+0800 CouponPurchase[49091:7083143] result -- #$Y$#
     2018-08-29 17:03:12.424471+0800 CouponPurchase[49091:7083143] string -- $Z$
     2018-08-29 17:03:12.424563+0800 CouponPurchase[49091:7083143] result -- #$Z$#
     */
}

非常完美,有没有。

既然匹配选项NSStringCompareOptions这么重要,那么就有必要把各个用途说明一下了。

api注释如下:

/* These options apply to the various search/find and comparison methods (except where noted).
*/
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 API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) = 128, /* If specified, ignores diacritics (o-umlaut == o) */
    NSWidthInsensitiveSearch API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) = 256, /* If specified, ignores width differences ('a' == UFF41) */
    NSForcedOrderingSearch API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.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 API_AVAILABLE(macos(10.7), ios(3.2), watchos(2.0), tvos(9.0)) = 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 */
};

英文看不懂的可以看别人的介绍

虽然这个选项有中、英文注释参考,却没有线索解释上面的现象。反正总结了一个使用规律:

如果使用NSRegularExpressionSearch,建议将要替换成的字符串前面拼接个字符串\\

猜你喜欢

转载自blog.csdn.net/jianghui12138/article/details/82187650