Replace characters (unique then wildcard value) in a string with other characters

后端 未结 1 330
予麋鹿
予麋鹿 2021-01-17 05:45

I have an NSString, let\'s say \"H,L,K,P\" how can I detect a specific character than then a wild-car character... for example, checking for \",*\" would return \",L\" \",K\

相关标签:
1条回答
  • 2021-01-17 06:29

    Use a regular expression. The search pattern would be:

    ,(.)
    

    the replacement pattern would be:

    ,$1.
    

    Sample code:

    NSString *string = @"H,L,K,P";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@",(.)"
                                        options:0
                                        error:&error];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:string
                                        options:0
                                        range:NSMakeRange(0, [string length])
                                        withTemplate:@",$1."];
    
    0 讨论(0)
提交回复
热议问题