源碼鏈接:https://github.com/qiushuai/SKTool,如果覺得還用的小伙伴記得Star
喲!N党觥5芮獭含懊!
秒數(shù)時間轉換
// 秒數(shù)時間轉換
+ (NSString *)distanceTimeWithBeforeTime:(double)time {
NSTimeInterval now = [[NSDate date]timeIntervalSince1970];
double distanceTime = now - time;
NSString * distanceStr;
NSDate * beDate = [NSDate dateWithTimeIntervalSince1970:time];
NSDateFormatter * df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"HH:mm"];
NSString * timeStr = [df stringFromDate:beDate];
[df setDateFormat:@"dd"];
NSString * nowDay = [df stringFromDate:[NSDate date]];
NSString * lastDay = [df stringFromDate:beDate];
if (distanceTime < 60) {
//小于一分鐘
distanceStr = @"剛剛";
} else if (distanceTime <60*60) {
//時間小于一個小時
distanceStr = [NSString stringWithFormat:@"%ld分鐘前",(long)distanceTime/60];
} else if (distanceTime <24*60*60 && [nowDay integerValue] == [lastDay integerValue]) {
//時間小于一天
distanceStr = [NSString stringWithFormat:@"今天 %@",timeStr];
} else if (distanceTime<24*60*60*2 && [nowDay integerValue] != [lastDay integerValue]){
if ([nowDay integerValue] - [lastDay integerValue] ==1 || ([lastDay integerValue] - [nowDay integerValue] > 10 && [nowDay integerValue] == 1)) {
distanceStr = [NSString stringWithFormat:@"昨天 %@",timeStr];
} else {
[df setDateFormat:@"MM-dd HH:mm"];
distanceStr = [df stringFromDate:beDate];
}
} else if (distanceTime <24*60*60*365) {
[df setDateFormat:@"MM-dd HH:mm"];
distanceStr = [df stringFromDate:beDate];
} else {
[df setDateFormat:@"yyyy-MM-dd HH:mm"];
distanceStr = [df stringFromDate:beDate];
}
return distanceStr;
}
隨機字符串
+ (NSString *)RandomString:(double)number {
NSString *string = [[NSString alloc]init];
for (int i = 0; i < number; i++) {
int number = arc4random() % 36;
if (number < 10) {
int figure = arc4random() % 10;
NSString *tempString = [NSString stringWithFormat:@"%d", figure];
string = [string stringByAppendingString:tempString];
} else {
int figure = (arc4random() % 26) + 97;
char character = figure;
NSString *tempString = [NSString stringWithFormat:@"%c", character];
string = [string stringByAppendingString:tempString];
}
}
return string;
}
判斷用戶輸入是否含有emoji
+ (BOOL)stringContainsEmoji:(NSString *)string {
__block BOOL returnValue = NO;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
const unichar hs = [substring characterAtIndex:0];
if (0xd800 <= hs && hs <= 0xdbff) {
if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f) {
returnValue = YES;
}
}
} else if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3) {
returnValue = YES;
}
} else {
if (0x2100 <= hs && hs <= 0x27ff) {
returnValue = YES;
} else if (0x2B05 <= hs && hs <= 0x2b07) {
returnValue = YES;
} else if (0x2934 <= hs && hs <= 0x2935) {
returnValue = YES;
} else if (0x3297 <= hs && hs <= 0x3299) {
returnValue = YES;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
returnValue = YES;
}
}
}];
return returnValue;
}
過濾emoji
+ (NSString *)removeStringIntheEmoji:(NSString *)string {
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]" options:NSRegularExpressionCaseInsensitive error:nil];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string
options:0
range:NSMakeRange(0, [string length])
withTemplate:@""];
return modifiedString;
}
查看App的當前版本號
+ (NSString *)getAppVersion {
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
return appVersion;
}
獲取App的build版本
+ (NSString *)getAppBuildVersion {
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *appBuildVersion = [infoDic objectForKey:@"CFBundleVersion"];
return appBuildVersion;
}
獲取App名稱
+ (NSString *)getAppName {
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *appName = [infoDic objectForKey:@"CFBundleDisplayName"];
return appName;
}
判斷是否為手機號
+ (BOOL)isValidPhoneWithString:(NSString *)phoneString
{
/**
* 手機號碼
* 移動:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
* 聯(lián)通:130,131,132,152,155,156,185,186
* 電信:133,1349,153,180,189
*/
NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
/**
10 * 中國移動:China Mobile
11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
12 */
NSString * CM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
/**
15 * 中國聯(lián)通:China Unicom
16 * 130,131,132,152,155,156,185,186
17 */
NSString * CU = @"^((13[0-2])|(145)|(15[5-6])|(17[0,6])|(16[6])|(18[5,6]))\\d{8}|(1709)\\d{7}$";
/**
* 中國電信:China Telecom
* 133,1349,153,180,189,177,173,181(增加)
*/
NSString * CT = @"^((133)|(153)|(17[7,3])|(19[8,9])|(18[0,1,9]))\\d{8}$";
/**
25 * 大陸地區(qū)固話及小靈通
26 * 區(qū)號:010,020,021,022,023,024,025,027,028,029
27 * 號碼:七位或八位
28 */
// NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
if (([regextestmobile evaluateWithObject:phoneString] == YES)
|| ([regextestcm evaluateWithObject:phoneString] == YES)
|| ([regextestct evaluateWithObject:phoneString] == YES)
|| ([regextestcu evaluateWithObject:phoneString] == YES))
{
return YES;
}
else
{
return NO;
}
}
把手機號第4-7位變成星號
+ (NSString *)phoneNumToAsterisk:(NSString*)phoneNum
{
if (![NSString isValidPhoneWithString:phoneNum]) {
return phoneNum;
}
return [phoneNum stringByReplacingCharactersInRange:NSMakeRange(3,4) withString:@"****"];
}
把手機號第4-11位變成星號
+ (NSString*)idCardToAsterisk:(NSString *)idCardNum
{
if (![NSString isValidPhoneWithString:idCardNum]) {
return idCardNum;
}
return [idCardNum stringByReplacingCharactersInRange:NSMakeRange(3, 8) withString:@"********"];
}
判斷字符串是否包含空格
+ (BOOL)isBlank:(NSString *)str
{
NSRange _range = [str rangeOfString:@" "];
if (_range.location != NSNotFound) {
return YES;
}
else {
return NO;
}
}
郵箱驗證
- (BOOL)isValidEmail
{
// 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:self];
NSString *emailRegex = @"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
return [self isValidateWithRegex:emailRegex];
}
車牌號驗證
- (BOOL)isValidCarNo
{
// NSString *carRegex = @"^[A-Za-z]{1}[A-Za-z_0-9]{5}$";
// NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
// return [carTest evaluateWithObject:self];
//車牌號:湘K-DE829 香港車牌號碼:粵Z-J499港
NSString *carRegex = @"^[\u4e00-\u9fff]{1}[a-zA-Z]{1}[-][a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fff]$";//其中\(zhòng)u4e00-\u9fa5表示unicode編碼中漢字已編碼部分,\u9fa5-\u9fff是保留部分衅胀,將來可能會添加
return [self isValidateWithRegex:carRegex];
}
網(wǎng)址驗證
- (BOOL)isValidUrl
{
NSString *regex = @"^((http)|(https))+:[^\\s]+\\.[^\\s]*$";
return [self isValidateWithRegex:regex];
}
是否郵政編碼
- (BOOL)isValidPostalcode {
NSString *phoneRegex = @"^[0-8]\\d{5}(?!\\d)$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:self];
}
是否純漢字
- (BOOL)isValidChinese;
{
NSString *phoneRegex = @"^[\u4e00-\u9fa5]+$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:self];
}
是否符合IP格式
- (BOOL)isValidIP;
{
NSString *regex = [NSString stringWithFormat:@"^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$"];
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
BOOL rc = [pre evaluateWithObject:self];
if (rc) {
NSArray *componds = [self componentsSeparatedByString:@","];
BOOL v = YES;
for (NSString *s in componds) {
if (s.integerValue > 255) {
v = NO;
break;
}
}
return v;
}
return NO;
}
銀行卡號有效性問題Luhn算法
/** 銀行卡號有效性問題Luhn算法
* 現(xiàn)行 16 位銀聯(lián)卡現(xiàn)行卡號開頭 6 位是 622126~622925 之間的岔乔,7 到 15 位是銀行自定義的,
* 可能是發(fā)卡分行滚躯,發(fā)卡網(wǎng)點雏门,發(fā)卡序號,第 16 位是校驗碼掸掏。
* 16 位卡號校驗位采用 Luhm 校驗方法計算:
* 1茁影,將未帶校驗位的 15 位卡號從右依次編號 1 到 15,位于奇數(shù)位號上的數(shù)字乘以 2
* 2丧凤,將奇位乘積的個十位全部相加募闲,再加上所有偶數(shù)位上的數(shù)字
* 3,將加法和加上校驗位能被 10 整除愿待。
*/
- (BOOL)isValidBankCard // 銀行卡驗證
{
NSString * lastNum = [[self substringFromIndex:(self.length-1)] copy];//取出最后一位
NSString * forwardNum = [[self substringToIndex:(self.length -1)] copy];//前15或18位
NSMutableArray * forwardArr = [[NSMutableArray alloc] initWithCapacity:0];
for (int i=0; i<forwardNum.length; i++) {
NSString * subStr = [forwardNum substringWithRange:NSMakeRange(i, 1)];
[forwardArr addObject:subStr];
}
NSMutableArray * forwardDescArr = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = (int)(forwardArr.count-1); i> -1; i--) {//前15位或者前18位倒序存進數(shù)組
[forwardDescArr addObject:forwardArr[i]];
}
NSMutableArray * arrOddNum = [[NSMutableArray alloc] initWithCapacity:0];//奇數(shù)位*2的積 < 9
NSMutableArray * arrOddNum2 = [[NSMutableArray alloc] initWithCapacity:0];//奇數(shù)位*2的積 > 9
NSMutableArray * arrEvenNum = [[NSMutableArray alloc] initWithCapacity:0];//偶數(shù)位數(shù)組
for (int i=0; i< forwardDescArr.count; i++) {
NSInteger num = [forwardDescArr[i] intValue];
if (i%2) {//偶數(shù)位
[arrEvenNum addObject:[NSNumber numberWithInteger:num]];
}else{//奇數(shù)位
if (num * 2 < 9) {
[arrOddNum addObject:[NSNumber numberWithInteger:num * 2]];
}else{
NSInteger decadeNum = (num * 2) / 10;
NSInteger unitNum = (num * 2) % 10;
[arrOddNum2 addObject:[NSNumber numberWithInteger:unitNum]];
[arrOddNum2 addObject:[NSNumber numberWithInteger:decadeNum]];
}
}
}
__block NSInteger sumOddNumTotal = 0;
[arrOddNum enumerateObjectsUsingBlock:^(NSNumber * obj, NSUInteger idx, BOOL *stop) {
sumOddNumTotal += [obj integerValue];
}];
__block NSInteger sumOddNum2Total = 0;
[arrOddNum2 enumerateObjectsUsingBlock:^(NSNumber * obj, NSUInteger idx, BOOL *stop) {
sumOddNum2Total += [obj integerValue];
}];
__block NSInteger sumEvenNumTotal =0 ;
[arrEvenNum enumerateObjectsUsingBlock:^(NSNumber * obj, NSUInteger idx, BOOL *stop) {
sumEvenNumTotal += [obj integerValue];
}];
NSInteger lastNumber = [lastNum integerValue];
NSInteger luhmTotal = lastNumber + sumEvenNumTotal + sumOddNum2Total + sumOddNumTotal;
return (luhmTotal%10 ==0)?YES:NO;
}
身份證驗證
- (BOOL)isValidIdCardNum
{
NSString *value = [self copy];
value = [value stringByReplacingOccurrencesOfString:@"X" withString:@"x"];
value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
int length = 0;
if (!value) {
return NO;
}else {
length = (int)value.length;
if (length != 15 && length !=18) {
return NO;
}
}
// 省份代碼
NSArray *areasArray =@[@"11", @"12", @"13", @"14", @"15", @"21", @"22", @"23", @"31", @"32", @"33", @"34", @"35", @"36", @"37", @"41", @"42", @"43", @"44", @"45", @"46", @"50", @"51", @"52", @"53", @"54", @"61", @"62", @"63", @"64", @"65", @"71", @"81", @"82", @"91"];
NSString *valueStart2 = [value substringToIndex:2];
BOOL areaFlag = NO;
for (NSString *areaCode in areasArray) {
if ([areaCode isEqualToString:valueStart2]) {
areaFlag = YES;
break;
}
}
if (!areaFlag) {
return NO;
}
NSRegularExpression *regularExpression;
NSUInteger numberofMatch;
int year = 0;
switch (length) {
case 15:
year = [value substringWithRange:NSMakeRange(6,2)].intValue +1900;
if (year % 4 ==0 || (year % 100 ==0 && year % 4 ==0)) {
regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$" options:NSRegularExpressionCaseInsensitive error:nil];// 測試出生日期的合法性
}else {
regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$" options:NSRegularExpressionCaseInsensitive error:nil];// 測試出生日期的合法性
}
numberofMatch = [regularExpression numberOfMatchesInString:value options:NSMatchingReportProgress range:NSMakeRange(0, value.length)];
if(numberofMatch > 0) {
return YES;
}else {
return NO;
}
case 18:
year = [value substringWithRange:NSMakeRange(6,4)].intValue;
if (year % 4 ==0 || (year % 100 ==0 && year % 4 ==0)) {
regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19|20[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"options:NSRegularExpressionCaseInsensitive error:nil];// 測試出生日期的合法性
}else {
regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19|20[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"
options:NSRegularExpressionCaseInsensitive error:nil];// 測試出生日期的合法性
}
numberofMatch = [regularExpression numberOfMatchesInString:value options:NSMatchingReportProgress range:NSMakeRange(0, value.length)];
if(numberofMatch > 0) {
int S = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3;
int Y = S % 11;
NSString *M = @"F";
NSString *JYM = @"10X98765432";
M = [JYM substringWithRange:NSMakeRange(Y,1)]; // 判斷校驗位
if ([M isEqualToString:[[value substringWithRange:NSMakeRange(17,1)] uppercaseString]]) {
return YES;// 檢測ID的校驗位
}else {
return NO;
}
}else {
return NO;
}
default:
return NO;
}
return NO;
}
判斷字符串中是否含有非法字符
+ (BOOL)isContainErrorCharacter:(NSString *)content {
NSString *str = @"^[A-Za-z0-9\\u4e00-\u9fa5]+$";
NSPredicate* emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
if (![emailTest evaluateWithObject:content]) {
return YES;
}
return NO;
}
判斷字符串中包含空格換行
+ (BOOL)isEmpty:(NSString* )string {
if (!string) {
return true;
}
else {
NSCharacterSet *chara = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString* str =[string stringByTrimmingCharactersInSet:chara];
if (str.length ==0) {
return true;
}
else{
return false;
}
}
}
截取字符串中的數(shù)字
+ (NSString *)getPhoneNumberFomat:(NSString *)number {
NSMutableArray *characters = [NSMutableArray array];
NSMutableString *mutStr = [NSMutableString string];
// 分離出字符串中的所有字符浩螺,并存儲到數(shù)組characters中
for (int i = 0; i < number.length; i ++) {
NSString *subString = [number substringToIndex:i + 1];
subString = [subString substringFromIndex:i];
[characters addObject:subString];
}
// 利用正則表達式,匹配數(shù)組中的每個元素仍侥,判斷是否是數(shù)字要出,將數(shù)字拼接在可變字符串mutStr中
for (NSString *b in characters) {
NSString *regex = @"^[0-9]*$";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];// 謂詞
BOOL isShu = [pre evaluateWithObject:b];// 對b進行謂詞運算
if (isShu) {
[mutStr appendString:b];
}
}
return mutStr;
}
獲取設備聯(lián)網(wǎng)IP地址
+ (NSString *)getIPAddress {
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);
return address;
}
獲取UDID唯一標識
+ (NSString *)getUDID
{
return [[NSUUID UUID] UUIDString];
}
去掉兩端空格和換行符
- (NSString *)stringByTrimmingBlank
{
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
正則去除HTML標簽
+ (NSString *)getNormalStringFilterHTMLString:(NSString *)htmlStr
{
NSString *normalStr = htmlStr.copy;
//判斷字符串是否有效
if (!normalStr || normalStr.length == 0 || [normalStr isEqual:[NSNull null]]) return @"";
//過濾正常標簽
NSRegularExpression *regularExpression=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>" options:NSRegularExpressionCaseInsensitive error:nil];
normalStr = [regularExpression stringByReplacingMatchesInString:normalStr options:NSMatchingReportProgress range:NSMakeRange(0, normalStr.length) withTemplate:@""];
//過濾占位符
NSRegularExpression *plExpression=[NSRegularExpression regularExpressionWithPattern:@"&[^;]+;" options:NSRegularExpressionCaseInsensitive error:nil];
normalStr = [plExpression stringByReplacingMatchesInString:normalStr options:NSMatchingReportProgress range:NSMakeRange(0, normalStr.length) withTemplate:@""];
//過濾空格
NSRegularExpression *spaceExpression=[NSRegularExpression regularExpressionWithPattern:@"^\\s*|\\s*$" options:NSRegularExpressionCaseInsensitive error:nil];
normalStr = [spaceExpression stringByReplacingMatchesInString:normalStr options:NSMatchingReportProgress range:NSMakeRange(0, normalStr.length) withTemplate:@""];
return normalStr;
}
過濾HTML標簽
+ (NSString *)removeStringIntheHTML:(NSString *)html
{
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<[^>]*>" options:NSRegularExpressionCaseInsensitive error:&error];
if (!error) {
html = [regex stringByReplacingMatchesInString:html options:0 range:NSMakeRange(0, html.length) withTemplate:@"$2$1"];
} else {
NSLog(@"%@", error);
}
NSArray *html_code = @[
@"\"", @"'", @"&", @"<", @">",
@"", @"?", @"¢", @"£", @"¤",
@"¥", @"|", @"§", @"¨", @"?",
@"a", @"?", @"?", @"", @"?",
@"ˉ", @"°", @"±", @"2", @"3",
@"′", @"μ", @"?", @"·", @"?",
@"1", @"o", @"?", @"?", @"?",
@"?", @"?", @"×", @"÷", @"à",
@"á", @"?", @"?", @"?", @"?",
@"?", @"?", @"è", @"é", @"ê",
@"?", @"ì", @"í", @"?", @"?",
@"D", @"?", @"ò", @"ó", @"?",
@"?", @"?", @"?", @"ù", @"ú",
@"?", @"ü", @"Y", @"T", @"?",
@"à", @"á", @"a", @"?", @"?",
@"?", @"?", @"?", @"è", @"é",
@"ê", @"?", @"ì", @"í", @"?",
@"?", @"e", @"?", @"ò", @"ó",
@"?", @"?", @"?", @"?", @"ù",
@"ú", @"?", @"ü", @"y", @"t",
@"?", @"?", @"?", @"?", @"?",
@"?", @"∈", @"?", @"?", @"∏",
@"∑", @"?", @"?", @"√", @"∝",
@"∞", @"∠", @"∧", @"∨", @"∩",
@"∪", @"∫", @"∴", @"~", @"?",
@"≈", @"≠", @"≡", @"≤", @"≥",
@"?", @"?", @"?", @"?", @"?",
@"⊕", @"?", @"⊥", @"?", @"Α",
@"Β", @"Γ", @"Δ", @"Ε", @"Ζ",
@"Η", @"Θ", @"Ι", @"Κ", @"Λ",
@"Μ", @"Ν", @"Ξ", @"Ο", @"Π",
@"Ρ", @"Σ", @"Τ", @"Υ", @"Φ",
@"Χ", @"Ψ", @"Ω", @"α", @"β",
@"γ", @"δ", @"ε", @"ζ", @"η",
@"θ", @"ι", @"κ", @"λ", @"μ",
@"ν", @"ξ", @"ο", @"π", @"ρ",
@"?", @"σ", @"τ", @"υ", @"φ",
@"χ", @"ψ", @"ω", @"?", @"?",
@"?", @"?", @"?", @"?", @"?",
@"?", @"?", @"?", @"?", @"",
@"", @"", @"", @"", @"",
@"", @"–", @"—", @"‘", @"’",
@"?", @"“", @"”", @"?", @"?",
@"?", @"?", @"…", @"‰", @"′",
@"″", @"?", @"?", @" ̄", @"€",
@"?", @"←", @"↑", @"→", @"↓",
@"?", @"?", @"?", @"?", @"?",
@"?", @"?", @"?", @"?", @"?",
@"?",
];
NSArray *code = @[
@""", @"'", @"&", @"<", @">",
@" ", @"¡", @"¢", @"£", @"¤",
@"¥", @"¦", @"§", @"¨", @"©",
@"ª", @"«", @"¬", @"­", @"®",
@"¯", @"°", @"±", @"²", @"³",
@"´", @"µ", @"¶", @"·", @"¸",
@"¹", @"º", @"»", @"¼", @"½",
@"¾", @"¿", @"×", @"÷", @"À",
@"Á", @"Â", @"Ã", @"Ä", @"Å",
@"Æ", @"Ç", @"È", @"É", @"Ê",
@"Ë", @"Ì", @"Í", @"Î", @"Ï",
@"Ð", @"Ñ", @"Ò", @"Ó", @"Ô",
@"Õ", @"Ö", @"Ø", @"Ù", @"Ú",
@"Û", @"Ü", @"Ý", @"Þ", @"ß",
@"à", @"á", @"â", @"ã", @"ä",
@"å", @"æ", @"ç", @"è", @"é",
@"ê", @"ë", @"ì", @"í", @"î",
@"ï", @"ð", @"ñ", @"ò", @"ó",
@"ô", @"õ", @"ö", @"ø", @"ù",
@"ú", @"û", @"ü", @"ý", @"þ",
@"ÿ", @"∀", @"∂", @"&exists;", @"∅",
@"∇", @"∈", @"∉", @"∋", @"∏",
@"∑", @"−", @"∗", @"√", @"∝",
@"∞", @"∠", @"∧", @"∨", @"∩",
@"∪", @"∫", @"∴", @"∼", @"≅",
@"≈", @"≠", @"≡", @"≤", @"≥",
@"⊂", @"⊃", @"⊄", @"⊆", @"⊇",
@"⊕", @"⊗", @"⊥", @"⋅", @"Α",
@"Β", @"Γ", @"Δ", @"Ε", @"Ζ",
@"Η", @"Θ", @"Ι", @"Κ", @"Λ",
@"Μ", @"Ν", @"Ξ", @"Ο", @"Π",
@"Ρ", @"Σ", @"Τ", @"Υ", @"Φ",
@"Χ", @"Ψ", @"Ω", @"α", @"β",
@"γ", @"δ", @"ε", @"ζ", @"η",
@"θ", @"ι", @"κ", @"λ", @"μ",
@"ν", @"ξ", @"ο", @"π", @"ρ",
@"ς", @"σ", @"τ", @"υ", @"φ",
@"χ", @"ψ", @"ω", @"ϑ", @"ϒ",
@"ϖ", @"Œ", @"œ", @"Š", @"š",
@"Ÿ", @"ƒ", @"ˆ", @"˜", @" ",
@" ", @" ", @"‌", @"‍", @"‎",
@"‏", @"–", @"—", @"‘", @"’",
@"‚", @"“", @"”", @"„", @"†",
@"‡", @"•", @"…", @"‰", @"′",
@"″", @"‹", @"›", @"‾", @"€",
@"™", @"←", @"↑", @"→", @"↓",
@"↔", @"↵", @"⌈", @"⌉", @"⌊",
@"⌋", @"◊", @"♠", @"♣", @"♥",
@"♦",
];
/*
NSArray *code_hex = @[
@""", @"'", @"&", @"<", @">",
@" ", @"¡", @"¢", @"£", @"¤",
@"¥", @"¦", @"§", @"¨", @"©",
@"ª", @"«", @"¬", @"­", @"®",
@"¯", @"°", @"±", @"²", @"³",
@"´", @"µ", @"¶", @"·", @"¸",
@"¹", @"º", @"»", @"¼", @"½",
@"¾", @"¿", @"×", @"÷", @"À",
@"Á", @"Â", @"Ã", @"Ä", @"Å",
@"Æ", @"Ç", @"È", @"É", @"Ê",
@"Ë", @"Ì", @"Í", @"Î", @"Ï",
@"Ð", @"Ñ", @"Ò", @"Ó", @"Ô",
@"Õ", @"Ö", @"Ø", @"Ù", @"Ú",
@"Û", @"Ü", @"Ý", @"Þ", @"ß",
@"à", @"á", @"â", @"ã", @"ä",
@"å", @"æ", @"ç", @"è", @"é",
@"ê", @"ë", @"ì", @"í", @"î",
@"ï", @"ð", @"ñ", @"ò", @"ó",
@"ô", @"õ", @"ö", @"ø", @"ù",
@"ú", @"û", @"ü", @"ý", @"þ",
@"ÿ", @"∀", @"∂", @"∃", @"∅",
@"∇", @"∈", @"∉", @"∋", @"∏",
@"∑", @"−", @"∗", @"√", @"∝",
@"∞", @"∠", @"∧", @"∨", @"∩",
@"∪", @"∫", @"∴", @"∼", @"≅",
@"≈", @"≠", @"≡", @"≤", @"≥",
@"⊂", @"⊃", @"⊄", @"⊆", @"⊇",
@"⊕", @"⊗", @"⊥", @"⋅", @"Α",
@"Β", @"Γ", @"Δ", @"Ε", @"Ζ",
@"Η", @"Θ", @"Ι", @"Κ", @"Λ",
@"Μ", @"Ν", @"Ξ", @"Ο", @"Π",
@"Ρ", @"Σ", @"Τ", @"Υ", @"Φ",
@"Χ", @"Ψ", @"Ω", @"α", @"β",
@"γ", @"δ", @"ε", @"ζ", @"η",
@"θ", @"ι", @"κ", @"Λ", @"μ",
@"Ν", @"ξ", @"ο", @"π", @"ρ",
@"ς", @"σ", @"τ", @"υ", @"φ",
@"χ", @"ψ", @"ω", @"ϑ", @"ϒ",
@"ϖ", @"Œ", @"œ", @"Š", @"š",
@"Ÿ", @"ƒ", @"ˆ", @"˜", @" ",
@" ", @" ", @"‌", @"‍", @"‎",
@"‏", @"–", @"—", @"‘", @"’",
@"‚", @"“", @"”", @"„", @"†",
@"‡", @"•", @"…", @"‰", @"′",
@"″", @"‹", @"›", @"‾", @"€",
@"™", @"←", @"↑", @"→", @"↓",
@"↔", @"↵", @"⌈", @"⌉", @"⌊",
@"⌋", @"◊", @"♠", @"♣", @"♥",
@"♦",
];
*/
NSInteger idx = 0;
for (NSString *obj in code) {
html = [html stringByReplacingOccurrencesOfString:(NSString *)obj withString:html_code[idx]];
idx++;
}
return html;
}
工商稅號
- (BOOL)isValidTaxNo
{
NSString *emailRegex = @"[0-9]\\d{13}([0-9]|X)$";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:self];
}
刪除字符串中的數(shù)字
+ (NSString *)stringDeleteNumber:(NSString *)str
{
NSMutableString *str1 = [NSMutableString stringWithString:str];
for (int i = 0; i < str1.length; i++) {
unichar c = [str1 characterAtIndex:i];
NSRange range = NSMakeRange(i, 1);
if ( c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9') { //此處可以是任何字符
[str1 deleteCharactersInRange:range];
--i;
}
}
NSString *newstr = [NSString stringWithString:str1];
return newstr;
}
數(shù)字如果前面有0,保留去掉0之后的數(shù)據(jù)
+ (NSString*)getTheCorrectNum:(NSString*)tempString
{
while ([tempString hasPrefix:@"0"])
{
tempString = [tempString substringFromIndex:1];
}
return tempString;
}
拼接字符串
- (NSString *)addString:(NSString *)str
{
return [NSString stringWithFormat:@"%@%@",self,str];
}
獲取當前時間戳
+ (NSString *)currentTimeStr
{
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//獲取當前時間0秒后的時間
NSTimeInterval time=[date timeIntervalSince1970];// *1000 是精確到毫秒农渊,不乘就是精確到秒
NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
return timeString;
}
刪除字符串最后一位字符
+ (NSString *)removeLastOneChar:(NSString *)origin {
NSString *cutted;
if([origin length] > 0){
cutted = [origin substringToIndex:([origin length]-1)];
}else{
cutted = origin;
}
return cutted;
}