44

I have a label that displays inches. I would like to display the number with the inch symbol (") or quotation mark. Can I do this with an nsstring? Thanks!

Jonah
  • 4,810
  • 14
  • 63
  • 76
  • 7
    Incidentally, the "inch" symbol is properly a double-prime (″) rather than a double-quote ("). Worst is when people use smart quotes for inches, I suppose. – Chuck Dec 20 '09 at 08:00
  • 1
    possible duplicate of [How to escape double quotes in string?](http://stackoverflow.com/questions/1352323/how-to-escape-double-quotes-in-string) – mmmmmm Jun 02 '15 at 10:26

7 Answers7

111

Sure, you just need to escape the quotation mark.

NSString *someString = @"This is a quotation mark: \"";
NSLog(@"%@", someString );

Output:

This is a quotation mark: "
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
Jeff Kelley
  • 19,021
  • 6
  • 70
  • 80
  • Awesome! That's what I was looking for! – Jonah Dec 20 '09 at 05:09
  • 39
    Everybody has to start someone. Besides, everyone forgets trivial things from time to time. The other day, I couldn't remember the string formatter for hex and I've been programming in C for mumble-mumble years. – TechZen Dec 20 '09 at 15:06
  • 4
    Message to any newbie coders who looked this up.. don't let anyone bully you when you have gaps in your knowledge base. Ask even if you feel stupid, especially online. – Magoo May 19 '16 at 21:53
17

You can use Double Quote Escape Sequence here. You need to escape it using a backslash :

NSString *str = @"Hello \"World\"";
NSLog(@"Output : %@",str);

Output : Hello "World"

There are some other Escape Sequences also. Take a look at it :

\b    Backspace
\f    Form Feed
\n    Newline
\t    Horizontal Tab
\v    Vertical Tab
\\    Backslash
\’    Single Quote
\”    Double Quote
\?    Question Mark
Bhavin
  • 27,155
  • 11
  • 55
  • 94
6

As use of back slash \" has already mentioned so I am answering different. You can use ASCII Code too.

ASCII Code of " (double quote) is 34.

 NSString *str = [NSString stringWithFormat:@"%cThis is a quotation mark: %c", 34, 34];
 NSLog(@"%@", str);

And Output is: "This is a quotation mark: "

Swift 4.0 Version

let str = String(format: "%cThis is a quotation mark: %c", 34, 34)
print(str)
TheTiger
  • 13,264
  • 3
  • 57
  • 82
4

SWIFT

let string = " TEST  \"  TEST "
println(string)

output in console is - TEST " TEST

Jiří Zahálka
  • 8,070
  • 2
  • 21
  • 17
3

Yes, you can include a quotation mark in an NSString literal using the backslash to escape it.

For example, to put the string Quote " Quote in a string literal, you would use this:

@"Quote \" Quote"

A backslash followed by a quotation mark simply inserts the quotation mark into the string.

John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
2

If the string is a literal string, then you can use the escape character to add a quotation mark inside a string.

NSString *string = @"16\"";
apaderno
  • 28,547
  • 16
  • 75
  • 90
0

Use the following code for Swift 5, Xcode 10.2

let myText = #"This is a quotation mark: ""#
print(myText)

Output:

This is a quotation mark: "

AtulParmar
  • 4,358
  • 1
  • 24
  • 45