iOS 对字符串进行操作的封装,用于判断字符串是否为空、切割字符串、获取字符串中中英文字符等。

#import 

@interface CZString : NSObject

#pragma mark - 判断空字符串
/** 判断一个对象是否为空的字符串或是否全为空格 */
+ (BOOL)isEmpty:(id)object;
/** 判断一个对象是否为空的字符串 */
+ (BOOL)isNil:(id)object;

/**
 *  格式化字符串,防止纯数字时字符串自动转换为 NSNumber 类型,同时防止空值
 *
 *  @param object 字符串或 NSNull 等空值
 *
 *  @return 格式化后的字符串,空时返回【@""】
 */
+ (NSString *)formatString:(id)object;

/**
 *  切割字符串到指定的字符长度
 *
 *  @param string 要切割的原字符串
 *  @param limit  指定的字符数
 *
 *  @return 返回一个最大长度为 limit 的字符串
 */
+ (NSString *)trimString:(NSString *)string limit:(NSInteger)limit;

/**
 *  根据不同语言切割字符串到指定的字节长度,即若切割英文字符串8个字节(字符数),则中文字符串为4个字符
 *
 *  @param string 要切割的原字符串
 *  @param limit  指定的字节长度
 *
 *  @return 返回一个最大长度为 limit 的字符串,中文字符串比英文的少一半字符
 */
+ (NSString *)trimMixString:(NSString *)string limit:(NSInteger)limit;

/** 十六进制字符串转换为十进制 */
+ (NSString *)stringFromHexString:(NSString *)hexString;

#pragma mark - 获取字符串中的有效字符
/** 验证字符串是否为中文 */
+ (BOOL)isValidateChineseString:(NSString *)string;
/** 获取一段字符串中的中文字 */
+ (NSArray *)getAStringOfChineseWord:(NSString *)string;
/** 获取一段字符串中的中文字和中文字符 */
+ (NSArray *)getAStringOfChineseCharacters:(NSString *)string;
/** 获取一段字符串中的中文字、数字、英文 */
+ (NSArray *)getAStringOfChineseWordNumberEnglish:(NSString *)string;

@end

#import "CZString.h"

@implementation CZString

+ (BOOL)isEmpty:(id)object {
    if ([object isKindOfClass:[NSNull class]]) {
        return YES;
    } else {
        NSString *str = (NSString *)object;
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimmedString = [str stringByTrimmingCharactersInSet:set];
        if (trimmedString.length == 0) {
            return YES;
        } else {
            return NO;
        }
    }
}

+ (BOOL)isNil:(id)object {
    if ([object isKindOfClass:[NSNull class]]) {
        return YES;
    } else {
        NSString *str = (NSString *)object;
        NSString *checkStr = [NSString stringWithFormat:@"%@", str];
        return [checkStr containsString:@"null"] || [checkStr isEqualToString:@""] || checkStr.length == 0 ? YES : NO;
    }
}

+ (NSString *)formatString:(id)object {
    if ([CZString isNil:object]) {
        return @"";
    } else {
        NSString *str = (NSString *)object;
        return [NSString stringWithFormat:@"%@", str];
    }
}

+ (NSString *)trimString:(NSString *)string limit:(NSInteger)limit {
    if (string == nil) {
        return string;
    }
    if (string.length > limit) {
        return [string substringToIndex:limit];
    } else {
        return string;
    }
}

+ (NSString *)trimMixString:(NSString *)string limit:(NSInteger)limit {
    if (string == nil) {
        return string;
    }
    NSMutableString *c = [NSMutableString new];
    NSInteger position = limit;
    for (int i = 0; i < string.length; i ++) {
        if (position == 0) {
            break;
        }
        unichar ch = [string characterAtIndex:i];
        if (0x4e00 < ch && ch < 0x9fff) {
            //若为汉字
            [c appendString:[string substringWithRange:NSMakeRange(i, 1)]];
            position = position - 2;
        } else {
            [c appendString:[string substringWithRange:NSMakeRange(i, 1)]];
            position = position - 1;
        }
    }
    return c;
}

/** 十六进制字符串转换为十进制 */
+ (NSString *)stringFromHexString:(NSString *)hexString {
    if (![hexString hasSuffix:@"0x"]) { // 需要补全加上十六进制的标记
        return [NSString stringWithFormat:@"%.0lu", strtoul([hexString UTF8String], 0, 16)];
    } else {
        return [NSString stringWithFormat:@"%.0lu", strtoul([[NSString stringWithFormat:@"0x%@", hexString] UTF8String], 0, 16)];
    }
}

+ (BOOL)isValidateChineseString:(NSString *)string {
    NSString *cnNameRegex = @"^\\s*[\\u4e00-\\u9fa5]{1,}[\\u4e00-\\u9fa5.·]{0,15}[\\u4e00-\\u9fa5]{1,}\\s*$";
    NSPredicate *cnNameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", cnNameRegex];
    return [cnNameTest evaluateWithObject:string];
}

+ (NSArray *)getAStringOfChineseWord:(NSString *)string {
    if (string == nil || [string isEqual:@""]) {
        return nil;
    }
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (int i = 0; i < string.length; i ++) {
        int a = [string characterAtIndex:i];
        if (a < 0x9fff && a > 0x4e00) {
            [arr addObject:[string substringWithRange:NSMakeRange(i, 1)]];
        }
    }
    return arr;
}

+ (NSArray *)getAStringOfChineseCharacters:(NSString *)string {
    if (string == nil || [string isEqual:@""]) {
        return nil;
    }
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (int i = 0; i < string.length; i ++) {
        NSRange range = NSMakeRange(i, 1);
        NSString *subStr = [string substringWithRange:range];
        const char *c = [subStr UTF8String];
        if (strlen(c) == 3) {
            [arr addObject:subStr];
        }
    }
    return arr;
}

+ (NSArray *)getAStringOfChineseWordNumberEnglish:(NSString *)string {
    if (string == nil || [string isEqual:@""]) {
        return nil;
    }
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (int i = 0; i < string.length; i ++) {
        int a = [string characterAtIndex:i];    // ASCII 码
        if (a < 0x9fff && a > 0x4e00) { // 中文
            [arr addObject:[string substringWithRange:NSMakeRange(i, 1)]];
        }
        else if (48 <= a && a <= 57) {  // 数字
            [arr addObject:[string substringWithRange:NSMakeRange(i, 1)]];
        }
        else if ((65 <= a && a <= 90) || (97 <= a && a <= 122)) {   // 英文
            [arr addObject:[string substringWithRange:NSMakeRange(i, 1)]];
        }
    }
    return arr;
}

@end

你可能感兴趣的:(iOS 对字符串进行操作的封装,用于判断字符串是否为空、切割字符串、获取字符串中中英文字符等。)