Search and replace substring
Objective-C defines a small but powerful set of extensions to the ANSI C programming language that enables sophisticated object-oriented programming. Objective-C is the native language for Cocoa programming—it’s the language that the frameworks are written in, and the language that most applications are written in. You can also use some other languages—such as Python and Ruby—to develop programs using the Cocoa frameworks. It’s useful, though, to have at least a basic understanding of Objective-C because Apple’s documentation and code samples are typically written in terms of this language.
In this example, we will show how to search and replace a string with another string. We will replace the word "fox" with "dog" in the phrase, "The quick brown fox jumped over the lazy fox." In order to do this, we will use stringByReplacingOccurrencesOfString:withString:
1 NSString * fullString ; 2 NSString * newString ; 3 NSString * searchString ; 4 NSString * replaceString ; 5 6 fullString = @"The quick brown fox jumped over the lazy fox" ; 7 searchString = @"fox" ; 8 relaceString = @"dog" ; 9 10 newString = [ fullString stringByReplacingOccurrencesOfString : searchString 11 withString :replaceString ] ;