prathumca
prathumca

Reputation: 410

How can I print "\n" using NSLog?

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

Basically I was 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

Upvotes: 0

Views: 1625

Answers (4)

triandicAnt
triandicAnt

Reputation: 1378

NSLog(@"Hello\\nWorld");

Output -

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

Upvotes: 0

hotpaw2
hotpaw2

Reputation: 70733

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.

Upvotes: 0

anon
anon

Reputation:

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

Upvotes: 2

BoltClock
BoltClock

Reputation: 724342

Escape it using another backslash.

NSLog(@"Hello\\nWorld");

Upvotes: 9

Related Questions