How to replace a character in Objective-C String for iPhone SDK?


To replace a character in objective C we will have to use the inbuilt function of Objective C string library, which replaces occurrence of a string with some other string that we want to replace it with.

To create a string in objective C we need to write −

NSString *str = @"tutori@als";

Now we have a choice of replacing characters in this string and creating new one, or modify this same string. In this example we will modify this string and print in the next line.

str = [str stringByReplacingOccurrencesOfString:@"@" withString:@""];
NSLog(@”%@”,str);

When we run the above code, str is replaced with “tutorials” which was previously “tutori@als”.

Updated on: 29-Jun-2020

662 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements