1

I am trying to find list of words , if matches i am replacing. the blow code works but it is not replacing if the matching word occurs more than one time.

And i think i need to use while instead of if loop here , but i am not able to make it to work.

I am struggling Please let me know

    NSString *mymessage = @"for you for your information at you your at fate";

    NSMutableArray *full_text_list = [[NSMutableArray alloc]init];
    [full_text_list addObject:@"for"];
    [full_text_list addObject:@"for your information"];
    [full_text_list addObject:@"you"];
    [full_text_list addObject:@"at"];

    NSMutableArray *short_text_list = [[NSMutableArray alloc]init];
    [short_text_list addObject:@"4"];
    [short_text_list addObject:@"fyi"];
    [short_text_list addObject:@"u"];
    [short_text_list addObject:@"@"];

    for(int i=0;i<[full_text_list count];i++)
    {
        NSRange range = [mymessage rangeOfString:[full_text_list objectAtIndex:i]];

        if(range.location != NSNotFound) {
            NSLog(@"%@ found", [full_text_list objectAtIndex:i]);
            mymessage = [mymessage stringByReplacingCharactersInRange:range withString:[short_text_list objectAtIndex:i]];
        }


    }
1

3 Answers 3

15

You don't have to reinvent the wheel; Cocoa does that for you...

Code :

NSString* message = @"for you for your information at you your at fate";

NSMutableArray* aList = [[NSMutableArray alloc] initWithObjects:@"for your information",@"for",@"you ",@"at ",nil];
NSMutableArray* bList = [[NSMutableArray alloc] initWithObjects:@"fyi",@"4",@"u ",@"@ ",nil];

for (int i=0; i<[aList count];i++)
{
    message = [message stringByReplacingOccurrencesOfString:[aList objectAtIndex:i] 
                                                 withString:[bList objectAtIndex:i]];
}

NSLog(@"%@",message);

HINT : We'll be replacing each and every one occurence of aList[0] with bList[0], aList[1] with bList[1], and so on... ;-)

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks for the reply but for your information is not replaced as fyi it replaced as inform@ion. Any thoughts?
I just edited the code above... there was a tiny mistake ("result" instead of "message"). It should be working now...
@user198725878 I didn't get that : WHAT was replaced with WHAT?
the text 'for your information' is not replaced as fyi it replaced as '4 ur inform@ion'. Any thoughts?
Well.. that's because "for" is higher up in the list, than "for your information"... that's why... If "for your information" (as it is a "superset" of other searches) and "fyi" are placed as the FIRST object of the arrays, it'll work... ;-)
|
3

Look at NSString's stringByReplacingOccurrencesOfString:withString: method.

Comments

2

Your string (myMessage) contains "for" word 3 times. When the for loop is executing first times it is replacing the "for" with "4", After first execution of for loop your string become like "4 you 4 your in4mation at you your at fate". Because "for your information" has become "4 your in4mation". It has replaced the third "for" also.

Same way it is replacing "at" also from "in4mation" word and becoming like "in4m@ion", And finally you are getting a new string like "4 u 4 ur in4m@ion @ u ur @ f@e".

My English is not so good, I hope you got my point.

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.