开发者

How can I print "\n" using NSLog?

开发者 https://www.devze.com 2023-01-14 15:16 出处:网络
How can I print \"\\n\" to the console by using NSLog or printf function? Basically I was开发者_Go百科 trying to print a String with value \"Hello\\nWorld\". My requirement is, I want to print it AS

How can I print "\n" to the console by using NSLog or printf function?

Basically I was开发者_Go百科 trying to print a String with value "Hello\nWorld". My requirement is, I want to print it AS IT IS. But when I tried to print, it was printing like,

Hello
World   

not as

Hello\nWorld


Escape it using another backslash.

NSLog(@"Hello\\nWorld");


Try "Hello\\nWorld". If you want a literal \ in a string you need to escape it with another \ .


I use C strings (except in the very rare case where I'm debugging something specific to Unicode):

NSLog( [ NSString stringWithFormat:@"%s", "Hello\nWorld" ] );

Make the above into a macro taking C parameters, and it becomes portable between C and Obj C.


NSLog(@"Hello\\nWorld");

Output -

2013-07-04 21:13:52.913 myProject[7408:303] Hello\nWorld
0

精彩评论

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