开发者

How to Compare Second Letter?

开发者 https://www.devze.com 2023-03-29 16:06 出处:网络
I Use RegexKitLite FrameWorks. NSString *str = [NSString stringWithString:@\"Welcome\"]; NSString *newStr [NSString string];

I Use RegexKitLite FrameWorks.

NSString *str = [NSString stringWithString:@"Welcome"];
NSString *newStr [NSString string];

//RegexKitLite.h reference.
newStr = [str stringByReplacingOccurrencesOfRegex:<How To Expression?> withString:@"?"];

I want "Welcome" convert to => "W??????"; More generally, 开发者_如何学编程for Apple , Banana Cake, Love, Peach, Watermelon... I want to covert

Apple => A???? 
Banana => B????? 
Cake => C??? 
Love => L??? 

I Make a These Pattern (HeadLetter only Show)... All Word is Stored In NSMutableArray So I access

[arr objectAtIndex:i] stringByReplacingOccurrencesOfString:<Expression> withString:@"?"];

How to compare Second letter With RegularExpression?


You don't need to use a regex for this. Simply do

NSString *str = [NSString stringWithString:@"Welcome"];
NSString *newStr = [str stringByReplacingOccurrencesOfString:@"Welcome" withString:@"W??????"];

For more options, also see the NSString method stringByReplacingOccurrencesOfString:withString:options:range:


For the more general case you describe, this can be achieved by similar NSString methods, e.g.

NSString *newStr = [[str substringToIndex:1] stringByPaddingToLength:str.length 
                                                          withString:@"?" 
                                                     startingAtIndex:0];


Looking at your question, I'm not sure why you're insisting on using reg-ex?

[NSString characterAtIndex:] will allow you to look at a particular character at any position within a string.

0

精彩评论

暂无评论...
验证码 换一张
取 消