在一个中英文混合的字符串中,怎么判断它的长度,按英文占一个字节,汉字两个字节的标准
比如“你好ni”,长度就是6
        “abcd” 长度是4
有什么好办法吗,介绍一下,谢谢

 

-(NSUInteger) unicodeLengthOfString: (NSString *) text {
    NSUInteger asciiLength = 0;

    for (NSUInteger i = 0; i < text.length; i++) {


        unichar uc = [text characterAtIndex: i];

        asciiLength += isascii(uc) ? 1 : 2;
    }

    NSUInteger unicodeLength = asciiLength / 2;

    if(asciiLength % 2) {
        unicodeLength++;
    }

    return unicodeLength;
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-12
  • 2021-11-02
猜你喜欢
  • 2021-06-11
  • 2021-09-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案