2011-08-18 34 views
0

我使用RegexKitLite FrameWorks。如何比較第二封信?

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

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

我想「歡迎」轉換爲=>「W ??????」;更一般地,爲AppleBanana CakeLovePeachWatermelon ...我想隱蔽

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

我做一個這些模式(僅HeadLetter顯示)......所有的字存儲在NSMutableArray的所以我訪問

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

如何比較第二個字母與RegularExpression?

+0

我想正則表達式(第二封信比較)不想回答[STR stringByReplacingOccurrencesOfString:@ 「歡迎」 withString:@ 「W ??????」]; –

回答

0

你並不需要使用正則表達式這一點。簡單地做

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

對於更多的選擇,也看到了NSString方法stringByReplacingOccurrencesOfString:withString:options:range:


對於您所描述的更一般的情況下,這可以通過類似NSString方法,例如可以實現

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

不只是我想模式正則表達式第二個字母比較... ^^ ;; 你的答案是新手答案......我想正則表達式(第二封信比較) –

+0

@bitmapdata:我不明白你的要求。你能舉一個更好的例子嗎?我的回答是你在問題中提出的問題,所以請更好地解釋你想要的。 – PengOne

+0

好吧,我有許多文字...例如蘋果,香蕉蛋糕,愛,桃,西瓜...... 我想隱蔽蘋果=>一個????香蕉=> B ????? Cake => C ???愛=> L ??? 我做一個這些模式(僅HeadLetter展)... 所有的字存儲在NSMutableArray的 所以我訪問[ARR objectAtIndex:我] stringByReplacingOccurrencesOfString: withString:@]; 「?」 你是否在售貨品?你知道RegexkitLite嗎? –

0

看看你的問題,我不確定你爲什麼要堅持使用reg-ex?

[NSString的characterAtIndex:]可以讓你看一個特定字符在字符串中的任何位置。