开发者

Comparing a string not working when it seems it should

开发者 https://www.devze.com 2023-02-03 18:57 出处:网络
When i NSLog the contents of my string it outputs: m3u8 as expected. Because thats what I parse into the string.

When i NSLog the contents of my string it outputs:

m3u8

as expected. Because thats what I parse into the string.

Later when I do some compare to check is the string equal to m3u8 they fail.

NSRange match;
match = [aRadio.streamType rangeOfString: @"m3u8"];
if (match.location == NSNotFound) 
{
    NSLog(@" m3u8 Not MATCHED: %@",aRadio.streamType);
    //break;
}

if ([aRadio.streamType compare:@"m3u8" ] == NSOrderedSame)
{

    NSLog(@" m3u8 DETECTED: %@",aRadio.streamType);
}
else 
{
    NSLog(@"NO m3u8 DETECTED %@",aRadio.streamType);
    [self createStreamer];
    [streamer start];
}

The NSLogs show that there is a match from the first check and no detection for the 2nd check.

I would expect them both to see that the string contains m3u8 as confirmed by what is output by Radio.streamType in the NSLogs.

This is how i declared stramType

@property (nonatomic, retain) NSString *streamType;

Anybody able to explain where this odd behavior is coming from?

Thanks -Code

EDIT

NSLog(@"Str len: %d",[aRadio.streamType length]); 

outputs 5 as the length.

I try and trim the string here of newlines

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 

if(!currentElementValue) 
{
    currentElementValue = [[NSMutableString alloc] initWithString:string];
}
else
{   
  开发者_StackOverflow中文版  [currentElementValue appendString:string];
    [currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet 
newlineCharacterSet]];
    [currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet 
whitespaceAndNewlineCharacterSet]];
    [currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet 
controlCharacterSet]];
    [currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet 
nonBaseCharacterSet]];
    NSLog(@"Processing Value: %@", currentElementValue);
}   
}

but NSLog(@"Processing Value: %@", currentElementValue); still drops to a newline to display whats contained in currentElementValue which is 'm3u8'

Thanks -Code


you may want to attempt literal comparisons using something like isEqualToString:. There are of course more advanced options which introduce parameters for locale, diacratics, case insensitive, and so on -- it depends on what you need.


try comparing strings by

if ([aRadio.streamType caseInsensitiveCompare:@"m3u8"] == NSOrderedSame)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号