Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a case insensitive string in objective-c iphone?

I have a long string of some characters. I want to replace some chars with other chars.

For example

string1="Hello WORLD12";
string2="world";

string1= search string2 in string1 and replace it; 
//need this method in objective c

string1="Hello world12"; 
like image 778
Sanchit Paurush Avatar asked Jun 04 '11 11:06

Sanchit Paurush


3 Answers

If by case insensitive you mean the lower case replacement, Ken Pespisa has your answer, but if case insensitivity is about your search string you can do this:

[mystring stringByReplacingOccurrencesOfString:@"searchString" withString:@"replaceString" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mystring length])];

for more info see documentation of:

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange;
like image 74
tsakoyan Avatar answered Nov 15 '22 11:11

tsakoyan


NSString *myString = @"select name SELECT college Select row";
[myString stringByReplacingOccurrencesOfString:@"select" withString:@"update" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [myString length])];
output: @"update name update college update row";
like image 21
nirmala Avatar answered Nov 15 '22 11:11

nirmala


You can call the NSString method stringByReplacingOccurrencesOfString:withString:

NSString *string1 = "Hello WORLD12";
NSString *string2 = "world";

NSString *string3 = [string1 stringByReplacingOccurrencesOfString:@"WORLD" withString:string2];
like image 24
Ken Pespisa Avatar answered Nov 15 '22 13:11

Ken Pespisa