0

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?

PengOne
  • 48,188
  • 17
  • 130
  • 149
bitmapdata.com
  • 9,572
  • 5
  • 35
  • 43
  • I Want Regex Pattern(Second Letter Compare) Not Want Answer [str stringByReplacingOccurrencesOfString:@"Welcome" withString:@"W??????"]; – bitmapdata.com Aug 18 '11 at 17:47

2 Answers2

0

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];
PengOne
  • 48,188
  • 17
  • 130
  • 149
  • Not Simply I want Pattern Regular Expression Second Letter Compare...^^;; You Answer is Novice Answer... I Want Regular Expression (Second Letter Compare) – bitmapdata.com Aug 18 '11 at 17:47
  • @bitmapdata: I do not understand what you are asking. Can you please give a better example? My answer does what you ask in the question, so please explain better what you want. – PengOne Aug 18 '11 at 17:50
  • Okay, I Have a Many Word... For Example Apple ,Banana Cake, Love, Peach, Watermelon... I Want 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: withString:@"?"]; Do You UnderStand? You Know RegexkitLite? – bitmapdata.com Aug 18 '11 at 17:55
  • @bitmapdata: Your question is much more clear now. I've edited the question to include your latest comment. Please see the edit to my question above. This does not use a regex, but does achieve the desired result. – PengOne Aug 18 '11 at 18:06
  • You're code is Occure Compile Error. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString stringByPaddingToLength:withString:startingAtIndex:]: out of range padIndex' – bitmapdata.com Aug 18 '11 at 18:08
  • @bitmapdata: The `startingAtIndex` value should have been `0` and not `1`. Try again with the correction. – PengOne Aug 18 '11 at 18:19
  • @bitmapdata: Glad to hear it. If this worked, then you can click on the little check mark to "accept" this answer as the one that worked best. You may want to go through your prior questions and do the same. Accepting answers is a good practice on SO to encourage people to continue answering your questions. – PengOne Aug 18 '11 at 18:23
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.

xyzzycoder
  • 1,831
  • 13
  • 19