Reputation: 399
i have NSString, were the content of NSString is from a url. i want to add a word in between this NSString followed by a particular word. were this NSString will be changing constantly
for example:
i am getting a data like" hi mac this is ios"
, this string may change constantly like this
" hi ios this mac" or hi this is mac ios"
, etc.
i want to add a word like "hello" followed by "mac" always
" hi mac hello this is ios"
" hi ios this mac hello"
"hi this is mac hello ios"
how can i do this
Upvotes: 1
Views: 549
Reputation: 46543
If you dont want to replace mac with mac hello, then you can do in this way
NSMutableString *string=[NSMutableString stringWithString:@"hi mac this is ios"];
NSRange range = [string rangeOfString:@"mac"];
[string insertString:@" hello" atIndex:range.location+3];
NSLog(@"str=%@",string);
Output :
str=hi mac hello this is ios
Upvotes: 0
Reputation: 935
NSString *str = @"Hi Mac this is";
if ([str rangeOfString:@"mac" options:NSCaseInsensitiveSearch].location != NSNotFound)
{
int location = [str rangeOfString:@"mac" options:NSCaseInsensitiveSearch].location;
NSMutableString *mstr = [[[NSMutableString alloc] initWithString:str] autorelease];
NSString *strtemp = @"mac";
[mstr replaceCharactersInRange:NSMakeRange(location, [strtemp length]) withString:[NSString stringWithFormat:@"%@ %@", strtemp, @"hello"]];
NSLog(@"test %@", mstr);
}
Upvotes: 0
Reputation: 27225
You can use :
string = [string stringByReplacingOccurrencesOfString:@"Mac" withString:@"Mac Hello"];
Upvotes: 1
Reputation: 1889
Try this...
Yourstring = [Yourstring stringByReplacingOccurrencesOfString:@"mac" withString:@"mac hello "];
Upvotes: 1