Jonah
Jonah

Reputation: 4800

Is it possible to include a quotation mark as part of an nsstring?

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!

Upvotes: 44

Views: 35221

Answers (7)

AtulParmar
AtulParmar

Reputation: 4570

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: "

Upvotes: 0

TheTiger
TheTiger

Reputation: 13354

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)

Upvotes: 6

Jiří Zahálka
Jiří Zahálka

Reputation: 8258

SWIFT

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

output in console is - TEST " TEST

Upvotes: 4

Bhavin
Bhavin

Reputation: 27225

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

Upvotes: 17

Jeff Kelley
Jeff Kelley

Reputation: 19071

Sure, you just need to escape the quotation mark.

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

Output:

This is a quotation mark: "

Upvotes: 111

avpaderno
avpaderno

Reputation: 29689

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\"";

Upvotes: 2

John Calsbeek
John Calsbeek

Reputation: 36497

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.

Upvotes: 3

Related Questions