首页 > 编程知识 正文

string类获取字符串长度的方法,cstring获取字符串长度

时间:2023-05-03 13:55:24 阅读:227205 作者:85

//首先先新建一个NSString的Category类

//获取NSString对应的字符长度- (int)chineseLength{ int asciiCount = 0; int nonAsciiCount = 0; for (NSUInteger i = 0; i < self.length; i++) { unichar c = [self tldbb:i]; if (isascii(c)) { asciiCount++; } else { nonAsciiCount++; } } int count = (asciiCount + 1) / 2 + nonAsciiCount; return count;}//获取指定字符长度字符对应的字符串- (NSString *)chineseSubStringWithMaxCount:(NSUInteger)maxCount{ NSUInteger maxCount1 = 2 * maxCount; NSUInteger count = 0; NSUInteger index = 0; for (NSUInteger i = 0; i < self.length; i++) { unichar c = [self tldbb:i]; if (isascii(c)) { count++; } else { count += 2; } if (count == maxCount1) { index = i + 1; break; } else if (count > maxCount1) { index = i; break; } } if (index == 0) { index = self.length; } NSString *subString = [self substringToIndex:index]; return subString;}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。