29

I want to replace the string "abc" to "def" each time it appears in my NSString object: "axcd abc amamam dff abc kdj abc"

How do I do that??

Thanks,

Sagiftw

1
  • For future reference, spaces between tags represents different tags. I've fixed it for you, but the proper objective C tag is "objective-c". Commented Aug 31, 2010 at 23:06

2 Answers 2

65

Try stringByReplacingOccurrencesOfString:withString:.

NSString* foo = @"axvc abc amamam dff abc kjd abc";
NSString* bar = [foo stringByReplacingOccurrencesOfString:@"abc" withString:@"def"];
NSLog("%@", bar);
Sign up to request clarification or add additional context in comments.

2 Comments

@KaranAlangat, are you sure about that? The documentation says that it's still available and not deprecated, both in the iOS sets and OS X sets.
@zneak It s a mistake from me. I did check in my Xcode a lot of time and it didn't work for me. Now a days my Xcode shows unwanted errors even in blank lines. It works fine .
2

Check out replacing regular expressions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.