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]];
        }


    }
user198725878
  • 6,266
  • 18
  • 77
  • 135
  • 2
    see this may be help you :http://stackoverflow.com/a/9544264/1126111 –  Mar 20 '12 at 12:36

3 Answers3

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... ;-)

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • Thanks for the reply but for your information is not replaced as fyi it replaced as inform@ion. Any thoughts? – user198725878 Mar 20 '12 at 12:23
  • I just edited the code above... there was a tiny mistake ("result" instead of "message"). It should be working now... – Dr.Kameleon Mar 20 '12 at 12:26
  • @user198725878 I didn't get that : WHAT was replaced with WHAT? – Dr.Kameleon Mar 20 '12 at 12:27
  • the text 'for your information' is not replaced as fyi it replaced as '4 ur inform@ion'. Any thoughts? – user198725878 Mar 20 '12 at 12:28
  • 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... ;-) – Dr.Kameleon Mar 20 '12 at 12:33
  • @user198725878 Remember : Always put first what you want to be replaced first (in other words, what may include other replacements, but has a higher priority over them). If, given the phrase "one cat and one dog", I want to replace "one cat" with "ONE CAT" and "one" (when it is alone) with "ONE", putting "one cat" first will give "ONE CAT and one dog". If I put "one" first in the list, it'll first replace "one" with "ONE" (giving us as a result "ONE cat and ONE dog") and when it goes on to "one cat", there will be nothing to replace... so, it's all a matter of prioritization... ;-) – Dr.Kameleon Mar 20 '12 at 12:37
  • @user198725878 OK, using the above example ("for you for your information at you your at fate"), how would you like it to be transformed? (Tell me and I'll post another complete answer based on it... – Dr.Kameleon Mar 20 '12 at 12:55
  • Atually i want that to be 4 u fyi @ u your @ fate.thanks for the reply – user198725878 Mar 21 '12 at 03:00
  • @user198725878 The Code above has been corrected. Run it, and you'll see it outputs EXACTLY what you want... ;-) – Dr.Kameleon Mar 21 '12 at 04:17
  • Actully both the arrays are dynamic and fetched from db, now i am trying to sort the array...NSSortDescriptor *sortDescriptor; sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"length" ascending:NO] autorelease]; NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; [full_text_list sortUsingDescriptors:sortDescriptors]; – user198725878 Mar 21 '12 at 04:24
  • I am able to sort the full_text_list array but i am not sure how to macth corresponding short_text_array element? pls let me know that how can i proceed – user198725878 Mar 21 '12 at 04:57
3

Look at NSString's stringByReplacingOccurrencesOfString:withString: method.

wbyoung
  • 22,383
  • 2
  • 34
  • 40
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.