//.h
//string是Null
+ (BOOL)stringIsNull:(NSString *)string;
+ (BOOL)stringesIsNull:(NSArray*)stringes;
//string是圖片
+ (BOOL)stringIsPicture:(NSString *)string;
//string是數(shù)字
+ (BOOL)stringIsNumber:(NSString *)string;
//string是小寫(xiě)字母
+ (BOOL)stringIsLowercase:(NSString *)string;
//string是大寫(xiě)字母
+ (BOOL)stringIsUppercase:(NSString *)string;
//string是字母
+ (BOOL)stringIsLetter:(NSString *)string;
//string是密碼(字母+數(shù)字)
+ (BOOL)stringIsPassword:(NSString *)string;
//string是手機(jī)號(hào)
+ (BOOL)stringIsPhone:(NSString *)string;
//string是郵箱
+ (BOOL)stringIsEmail:(NSString *)string;
//string是身份證
+ (BOOL)stringIsIdentifier:(NSString *)string;
//string是護(hù)照
+ (BOOL)stringIsPassport:(NSString *)string;
//string是信用卡
+ (BOOL)stringIsCreditCard:(NSString *)string;
//string是生日
+ (BOOL)stringIsBirthday:(NSString *)string;
//string是網(wǎng)址
+ (BOOL)stringIsHTTP:(NSString *)string;
//string是漢字
+ (BOOL)stringIsCHI:(NSString *)string;
//string包含漢字
+ (BOOL)stringIncludeCHI:(NSString *)string;
//提取string中的數(shù)字
+ (NSString *)stringSelectNumber:(NSString *)string;
//提取string中的字母
+ (NSString *)stringSelectLetter:(NSString *)string;
//.m
//為Null
+ (BOOL)stringIsNull:(NSString *)string {
string = kSTR(@"%@", string);
if ([string isKindOfClass:[NSNull class]] || [string isEqualToString:@""] || string.length == 0) {
return YES;
}else {
return NO;
}}
+ (BOOL)stringesIsNull:(NSArray*)stringes {
for (NSString *string in stringes) {
if ([self stringIsNull:string]) {
return YES;
}
}
return NO;
}
//圖片
+ (BOOL)stringIsPicture:(NSString *)string {
if ([string hasSuffix:@"jpg"] || [string hasSuffix:@"png"] || [string hasSuffix:@"jpeg"]) {
return YES;
}
return NO;
}
//數(shù)字
+ (BOOL)stringIsNumber:(NSString *)string {
const char *value = [string UTF8String];
long len = strlen(value);
for (int i = 0; i < len; i++) {
if(!(value[i] >= '0'&& value[i] <= '9')){
return FALSE;
}
}
return TRUE;
}
//小寫(xiě)字母
+ (BOOL)stringIsLowercase:(NSString *)string {
NSString? *mzRegex = @"^[a-z]+$";
NSPredicate? *mzPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",mzRegex];
return [mzPredicate evaluateWithObject:string];
}
//大寫(xiě)字母
+ (BOOL)stringIsUppercase:(NSString *)string {
NSString? *mzRegex = @"^[A-Z]+$";
NSPredicate? *mzPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",mzRegex];
return [mzPredicate evaluateWithObject:string];
}
//字母
+ (BOOL)stringIsLetter:(NSString *)string {
NSString? *mzRegex = @"^[a-zA-Z]+$";
NSPredicate? *mzPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",mzRegex];
return [mzPredicate evaluateWithObject:string];
}
//密碼(字母+數(shù)字)
+ (BOOL)stringIsPassword:(NSString *)string {
NSString? *mzRegex = @"^[a-zA-Z0-9]+$";
NSPredicate? *mzPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",mzRegex];
return [mzPredicate evaluateWithObject:string];
}
//手機(jī)號(hào)
+ (BOOL)stringIsPhone:(NSString *)string {
const char? *value = [string UTF8String];
long len = strlen(value);
if (len != 11) {
return FALSE;
}
if (![self stringIsNumber:string]) {
return FALSE;
}
NSString *preString = [[NSString stringWithFormat:@"%@",string] substringToIndex:2];
if ([preString isEqualToString:@"13"] ||
[preString isEqualToString: @"15"] ||
[preString isEqualToString: @"18"]) {
return TRUE;
} else {
return FALSE;
}
return TRUE;
}
//郵箱
+ (BOOL)stringIsEmail:(NSString *)string {
NSArray? *array = [string componentsSeparatedByString:@"."];
if ([array count] >= 4) {
return FALSE;
}
NSString? *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate? *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailRegex];
return [emailTest evaluateWithObject:string];
}
//身份證
+ (BOOL)stringIsIdentifier:(NSString *)string {
const int factor[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };//加權(quán)因子
const int checktable[] = { 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 };//校驗(yàn)值對(duì)應(yīng)表
const int LENGTH = 18;
const char *str = [[string lowercaseString] UTF8String];
NSInteger i;
NSInteger length = strlen(str);
BOOL result = TRUE;
/*
* identifier length is invalid
*/
if (15 != length && LENGTH != length) {
result = FALSE;
} else {
for (i = 1; i < length - 1; i++) {
if(!(str[i] >= '0' && str[i] <= '9')) {
result = FALSE;
break;
}
}
if (result) {
if(LENGTH == length) {
if (!((str[i] >= '0' && str[i] <= '9')||str[i] == 'X'||str[i] == 'x')) {
result = FALSE;
}
}
}
/*
* check sum for second generation identifier
*/
if (result && length == LENGTH) {
int i;
int *ids = malloc(sizeof(int)*LENGTH);
for (i = 0; i < LENGTH; i++) {
ids[i] = str[i] - 48;
}
int checksum = 0;
for (i = 0; i < LENGTH - 1; i ++ ) {
checksum += ids[i] * factor[i];
}
if (ids[17] == checktable[checksum%11]||
(str[17] == 'x' && checktable[checksum % 11] == 10)) {
result? = TRUE;
} else {
result? = FALSE;
} free(ids);
ids = NULL;
}
}
return result;
}
//護(hù)照
+ (BOOL)stringIsPassport:(NSString *)string {
const char *str = [string UTF8String];
char first = str[0];
NSInteger length = strlen(str);
if (!(first == 'P' || first == 'G')) {
return FALSE;
}
if (first == 'P') {
if (length != 8) {
return FALSE;
}
}
if (first == 'G') {
if (length != 9) {
return FALSE;
}
}
BOOL result = TRUE;
for (NSInteger i = 1; i < length; i++) {
if (!(str[i] >= '0' && str[i] <= '9')) {
result = FALSE;
break;
}
}
return result;
}
//信用卡
+ (BOOL)stringIsCreditCard:(NSString *)string {
BOOL result = TRUE;
NSInteger length = [string length];
if (length >= 13) {
result = [self stringIsNumber:string];
if (result) {
NSInteger twoDigitBeginValue = [[string substringWithRange:NSMakeRange(0, 2)]integerValue];
NSInteger threeDigitBeginValue = [[string substringWithRange:NSMakeRange(0, 3)]integerValue];
NSInteger fourDigitBeginValue = [[string substringWithRange:NSMakeRange(0, 4)]integerValue];
//Diner's Club
if (((threeDigitBeginValue >= 300 && threeDigitBeginValue <= 305)||
fourDigitBeginValue ==
3095||twoDigitBeginValue==36||twoDigitBeginValue==38) && (14 != length)) {
result = FALSE;
}
//VISA
else if([self isStartWithString:@"4"] && !(13 == length||16 == length)) {
result = FALSE;
}
//MasterCard
else if((twoDigitBeginValue >= 51||twoDigitBeginValue <= 55) && (16 != length)) {
result = FALSE;
}
//American Express
else if(([self isStartWithString:@"34"]||[self isStartWithString:@"37"]) &&
(15 != length)) {
result = FALSE;
}
//Discover
else if([self isStartWithString:@"6011"] && (16 != length)) {
result = FALSE;
}
else {
NSInteger begin = [[string substringWithRange:NSMakeRange(0, 6)]integerValue];
//CUP
if ((begin >= 622126 && begin <= 622925) && (16 != length)) {
result = FALSE;
}
//other
else {
result = TRUE;
}
}
}
if (result) {
NSInteger digitValue;
NSInteger checkSum = 0;
NSInteger index = 0;
NSInteger leftIndex;
//even length, odd index
if (0 == length%2) {
index = 0;
leftIndex = 1;
}
//odd length, even index
else {
index = 1;
leftIndex = 0;
}
while (index < length) {
digitValue = [[string substringWithRange:NSMakeRange(index, 1)]integerValue];
digitValue = digitValue*2;
if (digitValue >= 10) {
checkSum += digitValue/10 + digitValue%10;
} else {
checkSum += digitValue;
}
digitValue = [[string substringWithRange:NSMakeRange(leftIndex, 1)]integerValue];
checkSum += digitValue;
index += 2;
leftIndex += 2;
}
result = (0 == checkSum%10) ? TRUE:FALSE;
}
} else {
result = FALSE;
}
return result;
}
+ (BOOL)isStartWithString:(NSString *)string{
int temp = [string intValue];
switch (temp) {
case 4:
return NO;
break;
case 34:
return NO;
break;
case 37:
return NO;
break;
case 6011:
return NO;
break;
default:
break;
}
return YES;
}
//生日
+ (BOOL)stringIsBirthday:(NSString *)string {
BOOL result = FALSE;
if (string && 8 == [string length])
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMdd"];
NSDate *date = [formatter dateFromString:string];
if (date) {
result = TRUE;
}
}
return result;
}
//網(wǎng)址
+ (BOOL)stringIsHTTP:(NSString *)string {
if ([string containsString:@"http://"]) {
return YES;
}
return NO;
}
//漢字
+ (BOOL)stringIsCHI:(NSString *)string {
NSString *match = @"(^[\u4e00-\u9fa5]+$)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
return [predicate evaluateWithObject:string];
}
//包含漢字
+ (BOOL)stringIncludeCHI:(NSString *)string {
if ([self stringIsNull:string]) {
return NO;
}
for(int i=0; i< [string length];i++) {
int a = [string characterAtIndex:i];
if( a > 0x4e00 && a < 0x9fff) {
return YES;
}
}
return NO;
}
//提取string中的數(shù)字
+ (NSString *)stringSelectNumber:(NSString *)string {
return [string stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [string length])];
}
//提取string中的字母
+ (NSString *)stringSelectLetter:(NSString *)string {
return [string stringByReplacingOccurrencesOfString:@"[^A-z]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [string length])];
}
謝謝閱讀齐唆!