objective-c 是否可以将引号作为 nsstring 的一部分包含在内?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1934886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to include a quotation mark as part of an nsstring?
提问by Jonah
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!
我有一个显示英寸的标签。我想用英寸符号 (") 或引号来显示数字。我可以用 nsstring 来做到这一点吗?谢谢!
回答by Jeff Kelley
Sure, you just need to escape the quotation mark.
当然,你只需要转义引号。
NSString *someString = @"This is a quotation mark: \"";
NSLog(@"%@", someString );
Output:
输出:
This is a quotation mark: "
回答by Bhavin
You can use Double Quote Escape Sequencehere. You need to escape it using a backslash:
您可以在此处使用双引号转义序列。您需要使用反斜杠对其进行转义:
NSString *str = @"Hello \"World\"";
NSLog(@"Output : %@",str);
Output : Hello "World"
There are some other Escape Sequencesalso. 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
回答by TheTiger
As use of back slash \"has already mentioned so I am answering different. You can use ASCII Codetoo.
由于反斜杠\" 的使用已经提到,所以我的回答不同。您也可以使用ASCII 码。
ASCII Codeof " (double quote)is 34.
ASCII码的“(双引号)为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
斯威夫特 4.0 版本
let str = String(format: "%cThis is a quotation mark: %c", 34, 34)
print(str)
回答by Ji?í Zahálka
SWIFT
迅速
let string = " TEST \" TEST "
println(string)
output in console is - TEST " TEST
控制台中的输出是 - TEST " TEST
回答by John Calsbeek
Yes, you can include a quotation mark in an NSStringliteral using the backslash to escape it.
是的,您可以NSString使用反斜杠在文字中包含引号以将其转义。
For example, to put the string Quote " Quotein a string literal, you would use this:
例如,要将字符串Quote " Quote放入字符串文字中,您可以使用以下命令:
@"Quote \" Quote"
A backslash followed by a quotation mark simply inserts the quotation mark into the string.
反斜杠后跟引号只是将引号插入到字符串中。
回答by kiamlaluno
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\"";
回答by AtulParmar
Use the following code for Swift 5, Xcode 10.2
对Swift 5, Xcode 10.2使用以下代码
let myText = #"This is a quotation mark: ""#
print(myText)
Output:
输出:
This is a quotation mark: "
这是一个引号:"

