【问题标题】:Loop through unicode to NSString通过 unicode 循环到 NSString
【发布时间】:2014-04-18 04:23:41
【问题描述】:

我试图弄清楚如何循环并从 unicode 值创建一个 NSString。我已经想出了如何使用更短的代码来做到这一点:

    unichar buffer[26];
    unichar letter = 0x0041;
    for (int i = 0; i < 26; i++) {
        buffer[i] = letter;
        letter++;
    }
    NSLog(@"%@", [NSString stringWithCharacters: buffer length:26]);

这将得到字母表。我还知道如何处理更长的代码:

    NSString *A = @"\U0001F30A";
    NSString *B = @"\U0001F30B";
    NSString *C = @"\U0001F30C";
    NSLog(@"Emoji: %@ %@ %@", A, B, C);

获得 3 个表情符号。

我想要做的是获取 \U00011111 和 \U00022222 之间的所有 unicode 字符的字符串(随机示例)。这是我第一次处理 unicodes,这完全让我难过。任何代码 sn-ps 或指针将不胜感激。

【问题讨论】:

    标签: objective-c unicode nsstring


    【解决方案1】:

    并非所有介于 U+11111 和 U+22222 之间的值都在 Unicode 标准中定义 (比较http://www.unicode.org/Public/6.2.0/ucd/UnicodeData.txt),所以我拿 以不同的范围为例:

    uint32_t first = 0x1F300;
    uint32_t last  = 0x1F310;
    NSMutableString *string = [NSMutableString string];
    for (uint32_t unicode = first; unicode <= last; unicode++) {
        // Create a string from the (4 byte, little-endian) unicode value:
        NSString *tmp = [[NSString alloc] initWithBytes:&unicode length:4 encoding:NSUTF32LittleEndianStringEncoding];
    
        [string appendString:tmp];
    }
    NSLog(@"%@", string);
    

    输出:?????????????????

    【讨论】:

      猜你喜欢
      • 2014-08-30
      • 2011-11-27
      • 1970-01-01
      • 1970-01-01
      • 2011-02-05
      • 2016-12-27
      • 1970-01-01
      • 2015-04-10
      • 2015-05-21
      相关资源
      最近更新 更多