iOS判断字符串类型长度处理

来源:互联网 发布:linux 查cpu核数 编辑:程序博客网 时间:2024/06/06 09:21
<span id="transmark"></span>#import <Foundation/Foundation.h>@interface NSString (QDUtil)/** *@brief分享的文字字数计算 * *@param text 分享文字 * *@return个数 */- (NSInteger)qd_textNumber;/** *  计算字符串字符数 * *  @return 个数 */- (NSInteger)qd_unicodeTextNumber;/** *  计算字符串中的文字个数,英文、中文、表情文 * *  @param string 进行转换的字符串 * *  @return 全部的文字 */- (NSArray *)qd_wordsForString;/** *  截取从 0-n 的 unicode 编码的字符 * *  @return 截取后的字符串 */- (NSString *)qd_subStringToUnicodeIndex:(NSInteger)index;/** *@brief判断nsstring是否为数字 * *@param string string * *@return是非 */- (BOOL)qd_isPureInt;/** *  根据秒数返回"xx:xx"或"xx:xx:xx"格式的时间显示 * *  @param seconds 秒数 * *  @return 指定格式的输出字符串 */+ (NSString *)qd_stringForSeconds:(CGFloat)seconds;/** *  去除字符串前后空格 */- (NSString *)qd_stringByTrimmingWhitespaceCharacter;/** *  判断字符必须是中英文 */- (BOOL)qd_chineseWordValidate;@end

#import "NSString+QDUtil.h"@implementation NSString (QDUtil)- (NSInteger)qd_textNumber{    NSStringEncoding gbkEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);    NSUInteger length = [self lengthOfBytesUsingEncoding:gbkEncoding];    CGFloat number = length / 2.f;        return ceil(number);}- (NSInteger)qd_unicodeTextNumber{    NSInteger strlength = 0;    UInt16 *p = (UInt16 *)[self cStringUsingEncoding:NSUnicodeStringEncoding];    NSInteger len = [self length];    for (NSInteger i = 0; i < len; i++) {        if (*p) {            if (*p > 127) {                strlength += 2;            } else {                strlength += 1;            }        }        p++;    }    return strlength;}- (NSArray *)qd_wordsForString{    NSMutableArray *words = [[NSMutableArray alloc] init];        const char *str = [self cStringUsingEncoding:NSUTF8StringEncoding];        char *word;    for (int i = 0; i < strlen(str);) {        int len = 0;        if (str[i] >= 0xFFFFFFFC) {            len = 6;        } else if (str[i] >= 0xFFFFFFF8) {            len = 5;        } else if (str[i] >= 0xFFFFFFF0) {            len = 4;        } else if (str[i] >= 0xFFFFFFE0) {            len = 3;        } else if (str[i] >= 0xFFFFFFC0) {            len = 2;        } else if (str[i] >= 0x00) {            len = 1;        }                word = malloc(sizeof(char) * (len + 1));        for (int j = 0; j < len; j++) {            word[j] = str[j + i];        }        word[len] = '\0';        i = i + len;                NSString *oneWord = [NSString stringWithCString:word encoding:NSUTF8StringEncoding];        free(word);        [words addObject:oneWord];    }        return words;}- (NSString *)qd_subStringToUnicodeIndex:(NSInteger)index{    NSArray *words = [self qd_wordsForString];    NSMutableString *text = [NSMutableString string];    [words enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {        NSString *tempString = [NSString stringWithFormat:@"%@%@", text, obj];        if ([tempString qd_unicodeTextNumber] <= index) { // 是否达到或超过指定字数了            [text appendString:obj];        } else {            *stop = YES;        }    }];        return [text copy];}- (BOOL)qd_isPureInt{    NSScanner* scan = [NSScanner scannerWithString:self];    int val;    return [scan scanInt:&val] && [scan isAtEnd];}+ (NSString *)qd_stringForSeconds:(CGFloat)seconds{    if (seconds > 0) {        if (seconds < 60.0f) {            return [NSString stringWithFormat:@"00:%02.f", floor(seconds)];        } else if (seconds < 3600.0f) {            return [NSString stringWithFormat:@"%02.f:%02.f", (CGFloat)((NSInteger)seconds / 60), (CGFloat)((NSInteger)seconds % 60)];        }                return [NSString stringWithFormat:@"%02.f:%2.f:%02.f", (CGFloat)((NSInteger)seconds / 3600), (CGFloat)(((NSInteger)seconds % 3600) / 60), (CGFloat)((NSInteger)seconds % 60)];    }        return @"00:00";}- (NSString *)qd_stringByTrimmingWhitespaceCharacter{    NSString *temp = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];    return temp;}- (BOOL)qd_chineseWordValidate{    NSUInteger len = self.length;    for(int i = 0; i < len; i++) {        unichar a = [self characterAtIndex:i];        if(!((isalpha(a))             ||(isalnum(a))             ||((a >= 0x4e00 && a <= 0x9fa6))             )) {            return NO;        }    }    return YES;}@end

0 0
原创粉丝点击