NSString+AMString

// Call this class method to any string instance to get the Application Version number
+ (NSString *)applicationVersion
{
NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
NSString *version = [info objectForKey:(NSString*)kCFBundleVersionKey];
return version;
}

// Call this class method to any string instance to the get Application name
+ (NSString *)applicationName
{
NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
NSString *name = [info objectForKey:(NSString*)kCFBundleNameKey];
return name;

}
// decode your data into string with NSUTF8StringEncoding
+ (NSString *)getStringFromData:(NSData *)data
{
    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
}
// remove any whitespace and newline character comes before and after the actual string
- (NSString *)removeWhiteSpaces
{
NSString *trimmedString = [self stringByTrimmingCharactersInSet:
  [NSCharacterSet whitespaceAndNewlineCharacterSet]];
return trimmedString;
}

// This method will return a string by concatenating all the strings of array by a space if you want to add comma in place of space change @" " to @", ".
+ (NSString *)getStringFromArray:(NSArray *)array
{
    return [array componentsJoinedByString:@" "];
}

// This method is use to return an array by separating the string by white space.
- (NSArray *)getArray
{
    return [self componentsSeparatedByString:@" "];
}

// This method is use to compare two strings without considering the Case value.
- (BOOL)isEqualToStringIgnoreCase:(NSString *)string
{
    return [self compare:string options:NSCaseInsensitiveSearch] == NSOrderedSame;
}

// This method is use to check a particular substring in the self string. 
- (BOOL)containsString:(NSString *)subString
{
    return ([self rangeOfString:subString].location == NSNotFound) ? NO : YES;
}

// This method is use to check if a particular string contains only letters. 
- (BOOL)containsOnlyLetters
{
    NSCharacterSet *letterCharacterset = [[NSCharacterSet letterCharacterSet] invertedSet];
    return ([self rangeOfCharacterFromSet:letterCharacterset].location == NSNotFound);
}

// This method is use to check if a particular string contains only numbers. 
- (BOOL)containsOnlyNumbers
{
    NSCharacterSet *numbersCharacterSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
    return ([self rangeOfCharacterFromSet:numbersCharacterSet].location == NSNotFound);
}

// This method is use to check if a particular string contains letters and numbers.
- (BOOL)containsOnlyNumbersAndLetters
{
    NSCharacterSet *numAndLetterCharSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
    return ([self rangeOfCharacterFromSet:numAndLetterCharSet].location == NSNotFound);
}

// This method is use to Validate an Email
- (BOOL)isValidEmail
{
    NSString *regx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTestPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regx];
    return [emailTestPredicate evaluateWithObject:self];
}

// Is Valid Phone
- (BOOL)isVAlidPhoneNumber
{
    NSString *regex = @"[235689][0-9]{6}([0-9]{3})?";
    NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [test evaluateWithObject:self];
}

// This method is use to Validate an URL
- (BOOL)isValidUrl
{
    NSString *regex =@"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [urlTest evaluateWithObject:self];
}

// This method is use to Validate an Name
- (BOOL)isValidName
{
    NSCharacterSet * chars = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLKMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz"];
    NSCharacterSet * characterSetFromTextField = [NSCharacterSet characterSetWithCharactersInString:self];
    
    return [chars isSupersetOfSet:characterSetFromTextField];
}

Comments