123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- //
- // NSString+YLString.m
- // YoungCatogory
- //
- // Created by kuxuan on 2017/7/12.
- // Copyright © 2017年 kuxuan. All rights reserved.
- //
- #import "NSString+YLString.h"
- #import <CommonCrypto/CommonDigest.h>
- #import "NSData+YLData.h"
- @implementation NSString (YLString)
- + (NSString *)stringWithUUID
- {
- CFUUIDRef uuidObj = CFUUIDCreate(nil);//create a new UUID
-
- //get the string representation of the UUID
- NSString *uuidString = (__bridge_transfer NSString *)CFUUIDCreateString(nil, uuidObj);
-
- CFRelease(uuidObj);
- return uuidString;
- }
- - (NSString *)md5
- {
- const char *cStr = [self UTF8String];
- unsigned char result [CC_MD5_DIGEST_LENGTH];
- CC_MD5( cStr, (CC_LONG)strlen(cStr), result );
-
- return [NSString stringWithFormat: @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
- result[0], result[1],
- result[2], result[3],
- result[4], result[5],
- result[6], result[7],
- result[8], result[9],
- result[10], result[11],
- result[12], result[13],
- result[14], result[15]
- ];
- }
- + (NSString *)cachesPath
- {
- static dispatch_once_t onceToken;
- static NSString *cachedPath;
-
- dispatch_once(&onceToken, ^{
- cachedPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
- });
-
- return cachedPath;
- }
- + (NSString *)documentsPath
- {
- static dispatch_once_t onceToken;
- static NSString *cachedPath;
-
- dispatch_once(&onceToken, ^{
- cachedPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
- });
-
- return cachedPath;
- }
- #pragma mark Temporary Paths
- + (NSString *)temporaryPath
- {
- static dispatch_once_t onceToken;
- static NSString *cachedPath;
-
- dispatch_once(&onceToken, ^{
- cachedPath = NSTemporaryDirectory();
- });
-
- return cachedPath;
- }
- - (BOOL)hasString:(NSString * _Nonnull)substring {
- return [self hasString:substring caseSensitive:YES];
- }
- - (BOOL)hasString:(NSString *)substring caseSensitive:(BOOL)caseSensitive {
- if (caseSensitive) {
- return [self rangeOfString:substring].location != NSNotFound;
- } else {
- return [self.lowercaseString rangeOfString:substring.lowercaseString].location != NSNotFound;
- }
- }
- - (BOOL)isEmail {
- return [NSString isEmail:self];
- }
- + (BOOL)isEmail:(NSString * _Nonnull)email {
- NSString *emailRegEx = @"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$";
-
- NSPredicate *regExPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
- return [regExPredicate evaluateWithObject:[email lowercaseString]];
- }
- + (NSString * _Nonnull)convertToUTF8Entities:(NSString * _Nonnull)string {
- return [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[string
- stringByReplacingOccurrencesOfString:@"%27" withString:@"'"]
- stringByReplacingOccurrencesOfString:[@"%e2%80%99" capitalizedString] withString:@"’"]
- stringByReplacingOccurrencesOfString:[@"%2d" capitalizedString] withString:@"-"]
- stringByReplacingOccurrencesOfString:[@"%c2%ab" capitalizedString] withString:@"«"]
- stringByReplacingOccurrencesOfString:[@"%c2%bb" capitalizedString] withString:@"»"]
- stringByReplacingOccurrencesOfString:[@"%c3%80" capitalizedString] withString:@"À"]
- stringByReplacingOccurrencesOfString:[@"%c3%82" capitalizedString] withString:@"Â"]
- stringByReplacingOccurrencesOfString:[@"%c3%84" capitalizedString] withString:@"Ä"]
- stringByReplacingOccurrencesOfString:[@"%c3%86" capitalizedString] withString:@"Æ"]
- stringByReplacingOccurrencesOfString:[@"%c3%87" capitalizedString] withString:@"Ç"]
- stringByReplacingOccurrencesOfString:[@"%c3%88" capitalizedString] withString:@"È"]
- stringByReplacingOccurrencesOfString:[@"%c3%89" capitalizedString] withString:@"É"]
- stringByReplacingOccurrencesOfString:[@"%c3%8a" capitalizedString] withString:@"Ê"]
- stringByReplacingOccurrencesOfString:[@"%c3%8b" capitalizedString] withString:@"Ë"]
- stringByReplacingOccurrencesOfString:[@"%c3%8f" capitalizedString] withString:@"Ï"]
- stringByReplacingOccurrencesOfString:[@"%c3%91" capitalizedString] withString:@"Ñ"]
- stringByReplacingOccurrencesOfString:[@"%c3%94" capitalizedString] withString:@"Ô"]
- stringByReplacingOccurrencesOfString:[@"%c3%96" capitalizedString] withString:@"Ö"]
- stringByReplacingOccurrencesOfString:[@"%c3%9b" capitalizedString] withString:@"Û"]
- stringByReplacingOccurrencesOfString:[@"%c3%9c" capitalizedString] withString:@"Ü"]
- stringByReplacingOccurrencesOfString:[@"%c3%a0" capitalizedString] withString:@"à"]
- stringByReplacingOccurrencesOfString:[@"%c3%a2" capitalizedString] withString:@"â"]
- stringByReplacingOccurrencesOfString:[@"%c3%a4" capitalizedString] withString:@"ä"]
- stringByReplacingOccurrencesOfString:[@"%c3%a6" capitalizedString] withString:@"æ"]
- stringByReplacingOccurrencesOfString:[@"%c3%a7" capitalizedString] withString:@"ç"]
- stringByReplacingOccurrencesOfString:[@"%c3%a8" capitalizedString] withString:@"è"]
- stringByReplacingOccurrencesOfString:[@"%c3%a9" capitalizedString] withString:@"é"]
- stringByReplacingOccurrencesOfString:[@"%c3%af" capitalizedString] withString:@"ï"]
- stringByReplacingOccurrencesOfString:[@"%c3%b4" capitalizedString] withString:@"ô"]
- stringByReplacingOccurrencesOfString:[@"%c3%b6" capitalizedString] withString:@"ö"]
- stringByReplacingOccurrencesOfString:[@"%c3%bb" capitalizedString] withString:@"û"]
- stringByReplacingOccurrencesOfString:[@"%c3%bc" capitalizedString] withString:@"ü"]
- stringByReplacingOccurrencesOfString:[@"%c3%bf" capitalizedString] withString:@"ÿ"]
- stringByReplacingOccurrencesOfString:@"%20" withString:@" "];
- }
- + (NSString * _Nonnull)encodeToBase64:(NSString * _Nonnull)string {
- NSData *data = [string convertToNSData];
- return [data base64EncodedStringWithOptions:0];
- }
- - (NSString * _Nonnull)encodeToBase64 {
- return [NSString encodeToBase64:self];
- }
- + (NSString * _Nonnull)decodeBase64:(NSString * _Nonnull)string {
- NSData *data = [[NSData alloc] initWithBase64EncodedString:string options:0];
- return [data convertToUTF8String];
- }
- - (NSString * _Nonnull)decodeBase64 {
- return [NSString decodeBase64:self];
- }
- + (NSData * _Nonnull)convertToNSData:(NSString * _Nonnull)string {
- return [string dataUsingEncoding:NSUTF8StringEncoding];
- }
- - (NSData * _Nonnull)convertToNSData {
- return [NSString convertToNSData:self];
- }
- - (NSString * _Nonnull)sentenceCapitalizedString {
- if (![self length]) {
- return @"";
- }
- NSString *uppercase = [[self substringToIndex:1] uppercaseString];
- NSString *lowercase = [[self substringFromIndex:1] lowercaseString];
-
- return [uppercase stringByAppendingString:lowercase];
- }
- - (NSString * _Nonnull)dateFromTimestamp {
- NSString *year = [self substringToIndex:4];
- NSString *month = [[self substringFromIndex:5] substringToIndex:2];
- NSString *day = [[self substringFromIndex:8] substringToIndex:2];
- NSString *hours = [[self substringFromIndex:11] substringToIndex:2];
- NSString *minutes = [[self substringFromIndex:14] substringToIndex:2];
-
- return [NSString stringWithFormat:@"%@/%@/%@ %@:%@", day, month, year, hours, minutes];
- }
- - (NSString * _Nonnull)urlEncode {
- return [self URLEncode];
- }
- - (NSString * _Nonnull)URLEncode {
- return [self stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
- }
- - (NSString * _Nonnull)removeExtraSpaces {
- NSString *squashed = [self stringByReplacingOccurrencesOfString:@"[ ]+" withString:@" " options:NSRegularExpressionSearch range:NSMakeRange(0, self.length)];
- return [squashed stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
- }
- - (NSString * _Nonnull)stringByReplacingWithRegex:(NSString * _Nonnull)regexString withString:(NSString * _Nonnull)replacement {
- NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString options:NSRegularExpressionCaseInsensitive error:nil];
- return [regex stringByReplacingMatchesInString:self options:0 range:NSMakeRange(0, [self length]) withTemplate:@""];
- }
- - (NSString * _Nonnull)HEXToString {
- NSMutableString *newString = [NSMutableString string];
- NSArray *components = [self componentsSeparatedByString:@" "];
- for (NSString * component in components) {
- int value = 0;
- sscanf([component cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
- [newString appendFormat:@"%c", (char)value];
- }
- return newString;
- }
- - (NSString * _Nonnull)stringToHEX {
- NSUInteger len = [self length];
- unichar *chars = malloc(len * sizeof(unichar));
- [self getCharacters:chars];
-
- NSMutableString *hexString = [[NSMutableString alloc] init];
-
- for (NSUInteger i = 0; i < len; i++ ) {
- [hexString appendFormat:@"%02x", chars[i]];
- }
- free(chars);
-
- return hexString;
- }
- + (NSString * _Nonnull)generateUUID {
- CFUUIDRef theUUID = CFUUIDCreate(NULL);
- CFStringRef string = CFUUIDCreateString(NULL, theUUID);
- CFRelease(theUUID);
- return (__bridge_transfer NSString *)string;
- }
- - (BOOL)isUUID {
- NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" options:NSRegularExpressionCaseInsensitive error:nil];
- NSUInteger matches = [regex numberOfMatchesInString:self options:0 range:NSMakeRange(0, [self length])];
- return matches == 1;
- }
- - (BOOL)isUUIDForAPNS {
- NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9a-f]{32}$" options:NSRegularExpressionCaseInsensitive error:nil];
- NSUInteger matches = [regex numberOfMatchesInString:self options:0 range:NSMakeRange(0, [self length])];
- return matches == 1;
- }
- - (NSString * _Nonnull)convertToAPNSUUID {
- return [[[self stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""] stringByReplacingOccurrencesOfString:@"-" withString:@""];
- }
- - (NSString *)trimAllWhiteSpace
- {
- return [self stringByReplacingOccurrencesOfString:@"\\s" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, self.length)];
- }
- - (CGFloat)heightForWidth:(float)width andFont:(UIFont * _Nonnull)font {
- CGSize size = CGSizeZero;
- if (self.length > 0) {
- CGRect frame = [self boundingRectWithSize:CGSizeMake(width, 999999) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: font } context:nil];
- size = CGSizeMake(frame.size.width, frame.size.height + 1);
- }
- return size.height;
- }
- + (NSString *)getAppName{
- NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
- NSString *appName = [infoDict objectForKey:@"CFBundleDisplayName"];
- return appName;
- }
- @end
|