在 iOS开发中往往会用到很多正则表达式, 和平时简单好用的方法,总结一下 分享给大家
移除空格
-(NSString *)removeWhiteSpace
{
NSString *resultStr=@"";
NSArray *arr=[self componentsSeparatedByString:@" "];
for (NSString *str in arr)
{
resultStr = [resultStr stringByAppendingString:str];
}
return resultStr;
}
判断是否为浮点形:
- (BOOL)isPureFloatStr{
NSScanner* scan = [NSScanner scannerWithString:self];
float val;
return[scan scanFloat:&val] && [scan isAtEnd];
}
邮箱
+ (BOOL) validateEmail:(NSString *)email
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
手机号码验证
+ (BOOL) validateMobile:(NSString *)mobile
{
//手机号以13,14, 15,17,18开头,八个 \d 数字字符
NSString *phoneRegex = @"(^1[3|4|5|7|8][0-9]{9}$)";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:mobile];
}
5 计算文字宽高
+(CGSize)getStringSize:(float)fontSize withString:(NSString*)str andWidth:(CGFloat)width
{
return [str boundingRectWithSize:CGSizeMake(width, LONG_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil].size;
}
用户名 6到20 位
+ (BOOL) validateUserName:(NSString *)name{
NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
return [userNamePredicate evaluateWithObject:name];
}
密码 6到20 位
+ (BOOL) validatePassword:(NSString *)passWord{
NSString *passWordRegex = @"^(?![0-9]+$)(?![A-Za-z]+$)[0-9A-Za-z]{6,20}$";
NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
return [passWordPredicate evaluateWithObject:passWord];
}
身份证号
+ (BOOL) validateIdentityCard: (NSString *)identityCard{
BOOL flag;
if (identityCard.length <= 0) {
flag = NO;
return flag;
}
NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [identityCardPredicate evaluateWithObject:identityCard];
}
格式化时间
+(NSString *)fomartDate:(NSString *)Date{
long long time=[Date doubleValue];
NSDate *nd = [NSDate dateWithTimeIntervalSince1970:time];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy/MM/dd"];
NSString *dateString = [dateFormat stringFromDate:nd];
return dateString;
}
微信支付获取手机 IP
#import <ifaddrs.h>
#import <arpa/inet.h>
+ (NSString *)getPhoneIP {
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL) {
if(temp_addr->ifa_addr->sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
if ([address isEqualToString:@"error"])
{
address = @"192.168.1.1";
}
return address;
}
判断密码的强弱
-(BOOL)weakPswd{
NSString *pswdEx =@"^(?=.*\\d.*)(?=.*[a-zA-Z].*).{6,20}$";
NSPredicate *regExPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pswdEx];
return [regExPredicate evaluateWithObject:self];
}
判断字符串是否为空
+(BOOL) isBlankString:(NSString *)string {
if (string == nil || string == NULL) {
return YES;
}
if ([string isKindOfClass:[NSNull class]]) {
return YES;
}
if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
return YES;
}
return NO;
}
验证银行卡
+(BOOL)isValidCardNumber:(NSString *)cardNumber
{
NSString *digitsOnly = [self getDigitsOnly:cardNumber];
int sum = 0;
int digit = 0;
int addend = 0;
BOOL timesTwo = false;
for (int i = digitsOnly.length - 1.0; i >= 0; i--)
{
digit = [digitsOnly characterAtIndex:i] - '0';
if (timesTwo)
{
addend = digit * 2;
if (addend > 9) {
addend -= 9;
}
}
else {
addend = digit;
}
sum += addend;
timesTwo = !timesTwo;
}
int modulus = sum % 10;
return modulus == 0;
}
+(NSString *)getDigitsOnly:(NSString*)s{
NSString *digitsOnly = @"";
char c;
for (int i = 0; i < s.length; i++)
{
c = [s characterAtIndex:i];
if (isdigit(c))
{
digitsOnly =[digitsOnly stringByAppendingFormat:@"%c",c];
}
}
return digitsOnly;
}
是否为纯数字
-(BOOL)isNumberString
{
NSCharacterSet *numberSet = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *stringSet = [NSCharacterSet characterSetWithCharactersInString:self];
if ([numberSet isSupersetOfSet:stringSet])
{
return YES;
}
else
{
return NO;
}
}
校验护照
+ (BOOL) isValidPassport:(NSString*)value
{
const char *str = [value UTF8String];
char first = str[0];
NSInteger length = strlen(str);
if (!(first == 'P' || first == 'G'))
{
return FALSE;
}
if (first == 'P')
{
if (length != 8)
{
return FALSE;
}
}
if (first == 'G')
{
if (length != 9)
{
return FALSE;
}
}
BOOL result = TRUE;
for (NSInteger i = 1; i < length; i++)
{
if (!(str[i] >= '0' && str[i] <= '9'))
{
result = FALSE;
break;
}
}
return result;
}
网友评论