| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- //
- // NSString+PJR.m
- // Lib
- //
- // Created by Paritosh on 15/05/14.
- //
- // * All rights reserved.
- /*
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the <organization> nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
- #import "NSString+PJR.h"
- #import <CommonCrypto/CommonDigest.h>
- @implementation NSString (PJR)
- // Checking if String is Empty
- -(BOOL)isBlank
- {
- if ([[self removeWhiteSpacesFromString] isEqualToString:@""]) {
- return NO;
- }
-
- if (self.length<6) {
- return NO;
- }
-
- if (self.length>16) {
- return NO;
- }
-
- return YES;
- //return ([[self removeWhiteSpacesFromString] isEqualToString:@""]) || self.length<6 ? YES : NO;
- }
- //Checking if String is empty or nil
- -(BOOL)isValid
- {
- return ([[self removeWhiteSpacesFromString] isEqualToString:@""] || self == nil || [self isEqualToString:@"(null)"]) ? NO :YES;
- }
- // remove white spaces from String
- - (NSString *)removeWhiteSpacesFromString
- {
- NSString *trimmedString = [self stringByTrimmingCharactersInSet:
- [NSCharacterSet whitespaceAndNewlineCharacterSet]];
- return trimmedString;
- }
- // Counts number of Words in String
- - (NSUInteger)countNumberOfWords
- {
- NSScanner *scanner = [NSScanner scannerWithString:self];
- NSCharacterSet *whiteSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
-
- NSUInteger count = 0;
- while ([scanner scanUpToCharactersFromSet: whiteSpace intoString: nil]) {
- count++;
- }
-
- return count;
- }
- // If string contains substring
- - (BOOL)containsString:(NSString *)subString
- {
- return ([self rangeOfString:subString].location == NSNotFound) ? NO : YES;
- }
- // If my string starts with given string
- - (BOOL)isBeginsWith:(NSString *)string
- {
- return ([self hasPrefix:string]) ? YES : NO;
- }
- // If my string ends with given string
- - (BOOL)isEndssWith:(NSString *)string
- {
- return ([self hasSuffix:string]) ? YES : NO;
- }
- // Replace particular characters in my string with new character
- - (NSString *)replaceCharcter:(NSString *)olderChar withCharcter:(NSString *)newerChar
- {
- return [self stringByReplacingOccurrencesOfString:olderChar withString:newerChar];
- }
- // Get Substring from particular location to given lenght
- - (NSString*)getSubstringFrom:(NSInteger)begin to:(NSInteger)end
- {
- NSRange r;
- r.location = begin;
- r.length = end - begin;
- return [self substringWithRange:r];
- }
- // Add substring to main String
- - (NSString *)addString:(NSString *)string
- {
- if(!string || string.length == 0)
- return self;
- return [self stringByAppendingString:string];
- }
- // Remove particular sub string from main string
- -(NSString *)removeSubString:(NSString *)subString
- {
- if ([self containsString:subString])
- {
- NSRange range = [self rangeOfString:subString];
- return [self stringByReplacingCharactersInRange:range withString:@""];
- }
- return self;
- }
- // If my string contains ony letters
- - (BOOL)containsOnlyLetters
- {
- NSCharacterSet *letterCharacterset = [[NSCharacterSet letterCharacterSet] invertedSet];
- return ([self rangeOfCharacterFromSet:letterCharacterset].location == NSNotFound);
- }
- // If my string contains only numbers
- - (BOOL)containsOnlyNumbers
- {
- NSCharacterSet *numbersCharacterSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
- return ([self rangeOfCharacterFromSet:numbersCharacterSet].location == NSNotFound);
- }
- // If my string contains letters and numbers
- - (BOOL)containsOnlyNumbersAndLetters
- {
- NSCharacterSet *numAndLetterCharSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
- return ([self rangeOfCharacterFromSet:numAndLetterCharSet].location == NSNotFound);
- }
- // If my string is available in particular array
- - (BOOL)isInThisarray:(NSArray*)array
- {
- for(NSString *string in array) {
- if([self isEqualToString:string]) {
- return YES;
- }
- }
- return NO;
- }
- // Get String from array
- + (NSString *)getStringFromArray:(NSArray *)array
- {
- return [array componentsJoinedByString:@" "];
- }
- // Convert Array from my String
- - (NSArray *)getArray
- {
- return [self componentsSeparatedByString:@" "];
- }
- // Get My Application Version number
- + (NSString *)getMyApplicationVersion
- {
- NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
- NSString *version = [info objectForKey:@"CFBundleShortVersionString"];
- return version;
- }
- // Get My Application name
- + (NSString *)getMyApplicationName
- {
- NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
- NSString *name = [info objectForKey:@"CFBundleDisplayName"];
- return name;
- }
- // Convert string to NSData
- - (NSData *)convertToData
- {
- return [self dataUsingEncoding:NSUTF8StringEncoding];
- }
- // Get String from NSData
- + (NSString *)getStringFromData:(NSData *)data
- {
- return [[NSString alloc] initWithData:data
- encoding:NSUTF8StringEncoding];
-
- }
- // Is Valid Email
- - (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];
- }
- // Is Valid Phone
- - (BOOL)isVAlidPhoneNumber
- {
- NSString *regex = @"^((13[0-9])|(17[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
- NSPredicate *telNumPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
- return [telNumPre evaluateWithObject:self];
- }
- // Is Valid URL
- - (BOOL)isValidUrl
- {
- NSString *regex =@"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
- NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
- return [urlTest evaluateWithObject:self];
- }
- - (BOOL)isPassword{
- NSString * regex = @"^[A-Za-z0-9]{6,12}$";
- NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
- return [pred evaluateWithObject:self];
- }
- - (NSString*) urlEncodedString {
-
- CFStringRef encodedCFString = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
- (__bridge CFStringRef) self,
- nil,
- CFSTR("?!@#$^&%*+,:;='\"`<>()[]{}/\\| "),
- kCFStringEncodingUTF8);
-
- NSString *encodedString = [[NSString alloc] initWithString:(__bridge_transfer NSString*) encodedCFString];
-
- if(!encodedString)
- encodedString = @"";
-
- return encodedString;
- }
- - (NSString*) urlDecodedString {
-
- CFStringRef decodedCFString = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,
- (__bridge CFStringRef) self,
- CFSTR(""),
- kCFStringEncodingUTF8);
-
- // We need to replace "+" with " " because the CF method above doesn't do it
- NSString *decodedString = [[NSString alloc] initWithString:(__bridge_transfer NSString*) decodedCFString];
- return (!decodedString) ? @"" : [decodedString stringByReplacingOccurrencesOfString:@"+" withString:@" "];
- }
- -(NSString *)md5
- {
- const char *cStr = [self UTF8String];
- unsigned char result[CC_MD5_DIGEST_LENGTH];
- CC_MD5(cStr, (int)strlen(cStr), result); // This is the md5 call
- 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 *)sha1
- {
-
- const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
- NSData *data = [NSData dataWithBytes:cstr length:self.length];
-
- uint8_t digest[CC_SHA1_DIGEST_LENGTH];
-
- CC_SHA1(data.bytes, (int)data.length, digest);
-
- NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
-
- for(int i=0; i<CC_SHA1_DIGEST_LENGTH; i++) {
- [output appendFormat:@"%02x", digest[i]];
- }
-
- return output;
- }
- - (NSString*)base64Encode
- {
- //1.将需要加密的数据转成二进制,因为Base64的编码和解码都是针对二进制的
- NSData*data = [self dataUsingEncoding:NSUTF8StringEncoding];
- //2.把二进制数据编码之后,直接转成字符串
- NSString*encodeString = [data base64EncodedStringWithOptions:0];
- //3.返回结果returnencodeStr;
- return encodeString;
- }
- - (NSString*)base64Decode
- {
- if(self.length==0){
- return nil;
- }
- //1.把编码之后的字符串解码成二进制
- NSData*data = [[NSData alloc]initWithBase64EncodedString:self options:0];
- //2.把解码之后的二进制转换成字符串
- NSString*encodeString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- //3. 返回结果returndecodeStr;
- return encodeString;
- }
- - (NSString *)returnCompleteString
- {
- return ([self isKindOfClass:[NSNull class]] || [self isEqualToString:@"(null)"]) ? @"" : self;
- }
- @end
|