有關(guān)string的判斷

//.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])];

}


謝謝閱讀齐唆!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子幻锁,更是在濱河造成了極大的恐慌隔披,老刑警劉巖壮莹,帶你破解...
    沈念sama閱讀 211,376評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绕德,死亡現(xiàn)場(chǎng)離奇詭異殃姓,居然都是意外死亡翔冀,警方通過(guò)查閱死者的電腦和手機(jī)导街,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)纤子,“玉大人搬瑰,你說(shuō)我怎么就攤上這事】嘏穑” “怎么了泽论?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,966評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)卡乾。 經(jīng)常有香客問(wèn)我翼悴,道長(zhǎng),這世上最難降的妖魔是什么幔妨? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,432評(píng)論 1 283
  • 正文 為了忘掉前任鹦赎,我火速辦了婚禮,結(jié)果婚禮上误堡,老公的妹妹穿的比我還像新娘古话。我一直安慰自己,他們只是感情好埂伦,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,519評(píng)論 6 385
  • 文/花漫 我一把揭開(kāi)白布煞额。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪膊毁。 梳的紋絲不亂的頭發(fā)上胀莹,一...
    開(kāi)封第一講書(shū)人閱讀 49,792評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音婚温,去河邊找鬼描焰。 笑死,一個(gè)胖子當(dāng)著我的面吹牛栅螟,可吹牛的內(nèi)容都是我干的荆秦。 我是一名探鬼主播,決...
    沈念sama閱讀 38,933評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼力图,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼步绸!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起吃媒,我...
    開(kāi)封第一講書(shū)人閱讀 37,701評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤瓤介,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后赘那,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體刑桑,經(jīng)...
    沈念sama閱讀 44,143評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,488評(píng)論 2 327
  • 正文 我和宋清朗相戀三年募舟,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了祠斧。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,626評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡拱礁,死狀恐怖琢锋,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情觅彰,我是刑警寧澤吩蔑,帶...
    沈念sama閱讀 34,292評(píng)論 4 329
  • 正文 年R本政府宣布,位于F島的核電站填抬,受9級(jí)特大地震影響烛芬,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜飒责,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,896評(píng)論 3 313
  • 文/蒙蒙 一赘娄、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧宏蛉,春花似錦遣臼、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,742評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)鹏浅。三九已至,卻和暖如春屏歹,著一層夾襖步出監(jiān)牢的瞬間隐砸,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工蝙眶, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留季希,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,324評(píng)論 2 360
  • 正文 我出身青樓幽纷,卻偏偏與公主長(zhǎng)得像式塌,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子友浸,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,494評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容