2014-01-17 47 views
1

我有這種行爲,我不明白。rangeOfString不識別子字符串

NSString *title = [[PushNotifcationManager getPushNotificationSettingsForKey:@"settings"] objectForKey:@"title"]; 
NSLog(@"title: %@",title); //Outputs Test\ntest 
if([title rangeOfString:@"\n"].location == NSNotFound){ 
    //String contains no \n 
}else{ 
    //String contains \n 
} 

即使字符串包含明確 「\ n」 的[title rangeOfString:@"\n"].location == NSNotFound回報true。任何人都可以幫助我呢?

回答

2

NSLog("%@")不逃避字符串中反斜槓字符,所以輸出 Test\ntest意味着你的字符串包含一個反斜槓字符後跟一個n, 不是一個換行符。

因此,你必須在一個字符串字面量代表一個反斜槓字符來檢查

[title rangeOfString:@"\\n"] 

其中\\

+0

它的工作,謝謝! – dan