NSLineSeparatorCharacter

NSLineSeparatorCharacter

本文介绍了如何创建一个包含NSLineSeparatorCharacter的NSString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSString 很适合unicode。所以通常当我想创建一个包含unicode的字符串时,我可以直接将它直接放入一个字符串文字中,如下:

NSString is pretty unicode friendly. So normally when I want to create a string containing unicode, I can pop it directly into a string literal like so:

NSString *myString = @"Press ⌘Q to quit…";

但这不适用于使用行分隔符(AKA: NSLineSeparatorCharacter ,Unicode U + 2028,UTF-8 E2 80 A8)。编译器(正确)将此解释为换行符,这是C语法中的no-no。

But that doesn't work with using a line separator (AKA: 
NSLineSeparatorCharacter, Unicode U+2028, UTF-8 E2 80 A8). The compiler (correctly) interprets this as a line break, which is a no-no in C syntax.

-stringWithFormat:也没有帮助。尝试

NSString *myString = [NSString stringWithFormat@"This is%don two lines…", NSLineSeparatorCharacter];

给我的字符串This is8232on two lines ...。

gives me the string "This is8232on two lines…".

推荐答案

结果是 -stringWithFormat:是正确的方法。我只需要使用%C 作为替换,而不是%d NSLineSeparatorCharacter 是枚举(因此是整数),因此编译器认为%d 是我应该使用的。但%C 是Cocoa插入 unichar 类型的方式。有一个小转换...

Turns out -stringWithFormat: is the right way to go. I just need to use %C as the substitution instead of %d. NSLineSeparatorCharacter is an enumeration (and thus, integral), so the compiler thinks %d is what I should be using. But %C is Cocoa's way of inserting unichar types. With a little casting...

NSLog(@"This is%Con two lines…", (unichar)NSLineSeparatorCharacter);

像一个魅力一样工作。

注意下面的Ken的注释:您可以使用转义序列将字符直接嵌入到字符串文字中:

Also note Ken's comment below: you can embed the character directly in a string literal using an escape sequence:

@"This is\u2028on two lines…"

这篇关于如何创建一个包含NSLineSeparatorCharacter的NSString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 07:04