tags:

views:

246

answers:

2

I'm trying to compare the equality of two multi-line strings. I'm getting one of the strings from a web service, and the other I'm getting from iTunes via the Scripting Bridge. The strings from the web service are eventually transferred to iTunes, so if I do that and then re-compare the strings, ideally they'd be identical.

However, when comparing strings like this, it seems that isEqualToString: always returns non-equality. I'm testing this by testing equality of a string from iTunes that originally came from the web service, and a string directly from the web service.

Logging both strings to the Console produces output from both strings that appears identical. Logging the lengths of the strings produce identical lengths.

I've also tried comparing the strings using some other methods. For example, I converted them to ASCII strings to make sure it wasn't some Unicode issue:

        NSData *iTunesStringData = [[self iTunesString] dataUsingEncoding:NSASCIIStringEncoding
                                                     allowLossyConversion:YES];
        NSData *webServiceStringData = [[self webServiceString] dataUsingEncoding:NSASCIIStringEncoding
                                                     allowLossyConversion:YES];


        NSString *newiTunesString = [[[NSString alloc] initWithData:iTunesStringData encoding:NSASCIIStringEncoding] autorelease];
        NSString *newWebServiceString = [[[NSString alloc] initWithData:webServiceStringData encoding:NSASCIIStringEncoding] autorelease];

        BOOL result = [newiTunesString isEqualToString:newWebServiceString];

Same problem, not equal. I've tried comparing just the first character:

        NSComparisonResult result = [newiTunesString compare:newWebServiceString
                                                            options:NSLiteralSearch
                                                              range:NSMakeRange(0,1)
                                                             locale:[NSLocale currentLocale]];

Does not return NSOrderedSame. I've logged these first characters to the Console and they seem identical. I also considered differences in carriage returns, and tried replacing @"\r" with @"" in both strings before comparing, which doesn't work (and besides, that shouldn't affect equality of just the first character). I don't want to remove @"\n" characters because I want to preserve the multiple lines.

What's going on? Any other ideas?

A: 

Just guessing here but maybe use this clean up your strings

stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] 
Anders K.
A: 

It turns out this problem was related to line endings. But since I'm comparing multi-line strings, I didn't want to completely strip out the newlines. I normalized the line endings like so:

        NSString *normalizediTunesString = [[[self iTunesString] componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@"\n"];
        NSString *normalizedWebServiceString = [[[self webServiceString] componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@"\n"];

Then, comparing the strings via compare: worked as expected.

Simone Manganelli