在實際項目開發過程中,發現字元串使用頻率還是非常高的,NSString提供了很多相關的API,但是在開發過程中發現很多業務功能都是相同的。因此根據在開發過程中遇到的字元串使用場景,進行了簡單封裝。具體代碼如下: NSString+Category.h 代碼: NSString+Category.m ...
在實際項目開發過程中,發現字元串使用頻率還是非常高的,NSString提供了很多相關的API,但是在開發過程中發現很多業務功能都是相同的。因此根據在開發過程中遇到的字元串使用場景,進行了簡單封裝。具體代碼如下:
NSString+Category.h 代碼:
// // NSString+Category.h // myApplication // // Created by xujinzhong on 2018/3/13. // Copyright © 2018年 xujinzhong. All rights reserved. // #import <Foundation/Foundation.h> @interface NSString (Category) - (BOOL)isBlank; - (BOOL)isValid; - (NSString *)removeWhiteSpacesFromString; - (NSUInteger)countNumberOfWords; - (BOOL)containsString:(NSString *)subString; - (BOOL)isBeginsWith:(NSString *)string; - (BOOL)isEndssWith:(NSString *)string; - (NSString *)replaceCharcter:(NSString *)olderChar withCharcter:(NSString *)newerChar; - (NSString*)getSubstringFrom:(NSInteger)begin to:(NSInteger)end; - (NSString *)addString:(NSString *)string; - (NSString *)removeSubString:(NSString *)subString; - (BOOL)containsOnlyLetters; - (BOOL)containsOnlyNumbers; - (BOOL)containsOnlyNumbersAndLetters; - (BOOL)isInThisarray:(NSArray*)array; + (NSString *)getStringFromArray:(NSArray *)array; - (NSArray *)getArray; + (NSString *)getMyApplicationVersion; + (NSString *)getMyApplicationName; - (NSData *)convertToData; + (NSString *)getStringFromData:(NSData *)data; - (BOOL)isValidEmail; - (BOOL)isVAlidPhoneNumber; - (BOOL)isValidUrl; @end
NSString+Category.m 代碼:
// // NSString+Category.m // myApplication // // Created by xujinzhong on 2018/3/13. // Copyright © 2018年 xujinzhong. All rights reserved. // #import "NSString+Category.h" @implementation NSString (Category) //字元串是否為空 -(BOOL)isBlank { return ([[self removeWhiteSpacesFromString] isEqualToString:@""]) ? YES : NO; } //字元串為空或nil -(BOOL)isValid { return ([[self removeWhiteSpacesFromString] isEqualToString:@""] || self == nil || [self isEqualToString:@"(null)"]) ? NO :YES; } //在字元串中刪除空格 - (NSString *)removeWhiteSpacesFromString { NSString *trimmedString = [self stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; return trimmedString; } //字元串中包含單詞數 - (NSUInteger)countNumberOfWords { NSScanner *scanner = [NSScanner scannerWithString:self]; NSCharacterSet *whiteSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; NSUInteger count = 0; while ([scanner scanUpToCharactersFromSet: whiteSpace intoString: nil]) { count++; } return count; } // 字元串中是否包含子字元串 - (BOOL)containsString:(NSString *)subString { return ([self rangeOfString:subString].location == NSNotFound) ? NO : YES; } // 字元串從給定的字元串開始 - (BOOL)isBeginsWith:(NSString *)string { return ([self hasPrefix:string]) ? YES : NO; } // 字元串以給定的字元串結束 - (BOOL)isEndssWith:(NSString *)string { return ([self hasSuffix:string]) ? YES : NO; } // 用新字元替換字元串中的特定字元 - (NSString *)replaceCharcter:(NSString *)olderChar withCharcter:(NSString *)newerChar { return [self stringByReplacingOccurrencesOfString:olderChar withString:newerChar]; } // 從特定的位置獲取子字元串,以使其正確 - (NSString*)getSubstringFrom:(NSInteger)begin to:(NSInteger)end { NSRange r; r.location = begin; r.length = end - begin; return [self substringWithRange:r]; } // 將子字元串添加到主字元串 - (NSString *)addString:(NSString *)string { if(!string || string.length == 0) return self; return [self stringByAppendingString:string]; } // 從字元串中刪除指定字元串 -(NSString *)removeSubString:(NSString *)subString { if ([self containsString:subString]) { NSRange range = [self rangeOfString:subString]; return [self stringByReplacingCharactersInRange:range withString:@""]; } return self; } // 字元串只包含字母 - (BOOL)containsOnlyLetters { NSCharacterSet *letterCharacterset = [[NSCharacterSet letterCharacterSet] invertedSet]; return ([self rangeOfCharacterFromSet:letterCharacterset].location == NSNotFound); } // 字元串只包含數字 - (BOOL)containsOnlyNumbers { NSCharacterSet *numbersCharacterSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]; return ([self rangeOfCharacterFromSet:numbersCharacterSet].location == NSNotFound); } // 字元串中包含數字和字母 - (BOOL)containsOnlyNumbersAndLetters { NSCharacterSet *numAndLetterCharSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet]; return ([self rangeOfCharacterFromSet:numAndLetterCharSet].location == NSNotFound); } // 字元串包含在數組中 - (BOOL)isInThisarray:(NSArray*)array { for(NSString *string in array) { if([self isEqualToString:string]) { return YES; } } return NO; } // 數組轉換為字元串 + (NSString *)getStringFromArray:(NSArray *)array { return [array componentsJoinedByString:@" "]; } // 字元串轉換為數組 - (NSArray *)getArray { return [self componentsSeparatedByString:@" "]; } // 獲取應用程式版本 + (NSString *)getMyApplicationVersion { NSDictionary *info = [[NSBundle mainBundle] infoDictionary]; NSString *version = [info objectForKey:@"CFBundleVersion"]; return version; } // 獲取應用程式名稱 + (NSString *)getMyApplicationName { NSDictionary *info = [[NSBundle mainBundle] infoDictionary]; NSString *name = [info objectForKey:@"CFBundleDisplayName"]; return name; } // 字元串轉換為NSData - (NSData *)convertToData { return [self dataUsingEncoding:NSUTF8StringEncoding]; } // NSData轉換為字元串 + (NSString *)getStringFromData:(NSData *)data { return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; } // 是否為郵件格式 - (BOOL)isValidEmail { NSString *regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; NSPredicate *emailTestPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; return [emailTestPredicate evaluateWithObject:self]; } // 是否為電話格式 - (BOOL)isVAlidPhoneNumber { NSString *regex = @"[235689][0-9]{6}([0-9]{3})?"; NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; return [test evaluateWithObject:self]; } // 是否為URL格式 - (BOOL)isValidUrl { NSString *regex =@"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+"; NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; return [urlTest evaluateWithObject:self]; } @end