开发者

How to search sub string in NSString

开发者 https://www.devze.com 2023-01-23 20:24 出处:网络
I have a question describd below: I h开发者_开发百科ave UITextView showing following content: Maybe it\'s because he doesn\'t settle.

I have a question describd below:

I h开发者_开发百科ave UITextView showing following content:

Maybe it's because he doesn't settle. [Give funny anecdote about something the groom did not settle for. It should not be related to dating or marriage.] Or maybe it's something else. I can't put my finger on it, but I just never saw him settling down.

Well, one day that all changed. [Tell story about how the groom met the bride and what he told you after they met.] Yes, meeting [bride's name] changed everything.

[Groom's name] stopped sounding picky and started sounding interested. He knew that what he had in this woman was amazing―like nothing he had ever experienced before. After mere weeks, he was deeply in love; and there was no way out.

And you know what? He never turned back. And here he is today with his beautiful bride. Could there be a more perfect match?

So let that be a lesson to all of you loveless old maids here tonight. No matter how bad you think your situation is, it can't be as bad as [groom's name]'s; and he ended up with what he wanted most.

And with that, I will close. [Groom's name] and [bride's name], you two are both amazing people. Alone, you can accomplish great things. Alone, you are funny, intelligent, charming people.

Now have a look on the strings written between "[]", (square brackets). Initially, this text will in non editable format. I have a button on the top of the screen named as "Edit". Now what I have to do, When I click on the "Edit" button, there should pop ups showing text fields and when I enter data in the text fields then that text will replace the strings written between the "[]". For example, in above text there is a string "[Groom's name]" so when I Tap on the edit button then there will be pop up asking me "Enter the name of Groom", When I enter the name of groom in the text field then that name will replace the "[Groom's name]" with the text written by me in the textfield. For each strings written between the "[]" there should be a popup. Now my question is how I will track these square brackets and after that how will I replace them with the text entered by me in text field.

If I m clear then u will understand that in the above text I have to replace all the strings in bold italic format.

If I am not clear on any point please let me know. Thanks in advance


With NSString you can do that (combining -rangeOfString and – stringByReplacingCharactersInRange:withString:)

But don't hesitate to use RegexKitLite, then you'll have access to the power of regular expressions


Some clues to do that with NSString :

  1. search [ and get it position
  2. search ] and get it position
  3. get the string in this range
  4. replace characters in this range with the string you prompted


You'll want to avail yourself of one of the many Cocoa RegEx libraries that exist.


You're in luck. In iOS 4, we have the NSRegularExpression class.

0

精彩评论

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