Created
March 4, 2013 10:04
-
-
Save mmcbrear/5081218 to your computer and use it in GitHub Desktop.
NSString+Utils Category - MD5String
- isValidEmailAddress
- googleAddressSearchFormat
- phoneAppFriendlyNumber
- safariFriendlyURL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSString+Utils.m | |
// Invenias | |
// | |
// Created by Martin McBrearty on 15/01/2013. | |
// Copyright (c) 2013 Myello Digital Ltd. All rights reserved. | |
// | |
#import "NSString+Utils.h" | |
@implementation NSString (Utils) | |
- (NSString*) MD5String | |
{ | |
const char *cStr = [self UTF8String]; | |
unsigned char digest[16]; | |
CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call | |
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; | |
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) | |
[output appendFormat:@"%02x", digest[i]]; | |
return output; | |
} | |
- (BOOL)isValidEmailAddress | |
{ | |
NSError *error = NULL; | |
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\b[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}\\b" | |
options:NSRegularExpressionCaseInsensitive | |
error:&error]; | |
NSUInteger numberOfMatches = [regex numberOfMatchesInString:self options:0 range:NSMakeRange(0, [self length])]; | |
return (numberOfMatches > 0) ? YES : NO; | |
} | |
- (NSString*) googleAddressSearchFormat | |
{ | |
NSString *result = [self stringByReplacingOccurrencesOfString:@" " withString:@"+"]; | |
return [result stringByReplacingOccurrencesOfString:@"\n" withString:@","]; | |
} | |
- (NSString*) phoneAppFriendlyNumber | |
{ | |
NSError *error = NULL; | |
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z]|[ \\-+,!@£$%^&*(){}\?.<>~`;:'|/\"\\[\\]\\\\]" | |
options:NSRegularExpressionCaseInsensitive | |
error:&error]; | |
if(!error) | |
return [regex stringByReplacingMatchesInString:self options:0 range:NSMakeRange(0, [self length]) withTemplate:@""]; | |
return self; | |
} | |
- (NSString*) safariFriendlyURL | |
{ | |
NSRange range = [self rangeOfString:@"http://"]; | |
if(range.location == NSNotFound) | |
return [NSString stringWithFormat:@"http://%@",self]; | |
return self; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment