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!
-
7Incidentally, 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
-
1possible 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 Answers
Sure, you just need to escape the quotation mark.
NSString *someString = @"This is a quotation mark: \"";
NSLog(@"%@", someString );
Output:
This is a quotation mark: "

- 242,470
- 58
- 448
- 498

- 19,021
- 6
- 70
- 80
-
-
39Everybody 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
-
4Message 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
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

- 27,155
- 11
- 55
- 94
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)

- 13,264
- 3
- 57
- 82
SWIFT
let string = " TEST \" TEST "
println(string)
output in console is - TEST " TEST

- 8,070
- 2
- 21
- 17
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.

- 35,947
- 7
- 94
- 101
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\"";

- 28,547
- 16
- 75
- 90
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: "

- 4,358
- 1
- 24
- 45