程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> NSString NSDate NSArray常用方法

NSString NSDate NSArray常用方法

編輯:C++入門知識

NSString NSDate NSArray常用方法


方法聲明及注釋:

#pragma mark 字符串相關方法

//轉換json

-(NSString *)jsonFromObject:(id)obj;

//判斷字符串

-(BOOL)isString:(NSString *)string;

//字典賦值

-(NSString *)dictionaryWithDic:(NSDictionary *)dic valueForKey:(NSString *)key;

//string轉換成date

-(NSDate *)dateFromString:(NSString *)dateString;

//date轉換成string

-(NSString *)stringFromDate:(NSDate *)date;

//時間戳轉換成字符串

-(NSString *)stringFromValue:(NSString *)value;

//驗證郵箱格式

-(BOOL)isValidateEmail:(NSString *)email;

//驗證電話號碼

- (BOOL)isValidateMobile:(NSString *)mobileNum;

//判斷是否為整形

- (BOOL)isPureInt:(NSString*)string;

//判斷是否為浮點形:


- (BOOL)isPureFloat:(NSString*)string;

//保留幾位小數

-(NSString *)newFloat:(float)value withNumber:(int)numberOfPlace;

//文本框賦值

-(NSString *)setText:(NSString *)text;

-(NSString *)setNumberText:(NSNumber *)number Unit:(NSString *)unit;

-(NSArray *)arraySorting:(NSString *)parameters Array:(NSArray *)array Methods:(BOOL)methods;//yes 是正序 ;no 是倒序


方法實現:

-(NSString *)jsonFromObject:(id)obj{

NSString *jsonString = nil;

NSError *error;

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:obj

options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string

error:&error];

if (! jsonData) {

NSLog(@"Got an error: %@", error);

} else {

jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

}

return jsonString;

}

#pragma mark 字符串相關方法

-(BOOL)isString:(NSString *)string{//字符串是否為空

if (![string isKindOfClass:[NSNull class]]&&string&&![string isEqualToString:@"(null)"]&& string.length != 0 &&![string isEqualToString:@""]) {

return YES;

}else{

return NO;

}

}

-(NSString *)dictionaryWithDic:(NSDictionary *)dic valueForKey:(NSString *)key{//字典取值

NSString * value = nil;

if (dic!=nil&&[[dic allKeys] containsObject:key]) {

value = [dic valueForKey:key];

}else{

value = @"";

}

return value;

}

- (NSDate *)dateFromString:(NSString *)dateString{

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];

NSDate *destDate= [dateFormatter dateFromString:dateString];

return destDate;

}


- (NSString *)stringFromDate:(NSDate *)date{

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

//zzz表示時區,zzz可以刪除,這樣返回的日期字符將不包含時區信息。

[dateFormatter setDateFormat:@"MM-dd HH:mm"];

NSString *destDateString = [dateFormatter stringFromDate:date];

return destDateString;

}

-(NSString *)stringFromValue:(NSString *)value{

NSString *str = [value substringToIndex:10];


NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[str longLongValue]];

NSDateFormatter* formatter = [[NSDateFormatter alloc]init];

[formatter setDateFormat:@"MM-dd HH:mm"];

NSString *date = [formatter stringFromDate:confromTimesp];

NSDate *now = [NSDate date];

NSString *dateNow = [formatter stringFromDate:now];

if ([[dateNow substringToIndex:5] isEqualToString:[date substringToIndex:5]]) {

date = @" 今天";

}

NSLog(@"當前時間:%@ 加載時間%@ %@",dateNow, date,[dateNow substringToIndex:5]);

return date;

}

/*郵箱驗證 MODIFIED BY HELENSONG*/

-(BOOL)isValidateEmail:(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];

}

/*手機號碼驗證 MODIFIED BY HELENSONG*/

- (BOOL)isValidateMobile:(NSString *)mobileNum

{

/**

* 手機號碼

* 移動:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188

* 聯通: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 = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";

/**

15 * 中國聯通:China Unicom

16 * 130,131,132,152,155,156,185,186

17 */

NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";

/**

20 * 中國電信:China Telecom

21 * 133,1349,153,180,189

22 */

NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";

/**

25 * 大陸地區固話及小靈通

26 * 區號: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:mobileNum] == YES)

|| ([regextestcm evaluateWithObject:mobileNum] == YES)

|| ([regextestct evaluateWithObject:mobileNum] == YES)

|| ([regextestcu evaluateWithObject:mobileNum] == YES))

{

return YES;

}

else

{

return NO;

}

}


/*車牌號驗證 MODIFIED BY HELENSONG*/

BOOL validateCarNo(NSString* carNo)

{

NSString *carRegex = @"^[A-Za-z]{1}[A-Za-z_0-9]{5}$";

NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];

NSLog(@"carTest is %@",carTest);

return [carTest evaluateWithObject:carNo];

}

//判斷是否為整形

- (BOOL)isPureInt:(NSString*)string{

NSScanner* scan = [NSScanner scannerWithString:string];

int val;

return[scan scanInt:&val] && [scan isAtEnd];

}


//判斷是否為浮點形:


- (BOOL)isPureFloat:(NSString*)string{

NSScanner* scan = [NSScanner scannerWithString:string];

float val;

return[scan scanFloat:&val] && [scan isAtEnd];

}

//保留幾位小數

-(NSString *)newFloat:(float)value withNumber:(int)numberOfPlace

{

NSString *formatStr = @"%0.";

formatStr = [formatStr stringByAppendingFormat:@"%df", numberOfPlace];

formatStr = [NSString stringWithFormat:formatStr, value];

printf("formatStr %s\n", [formatStr UTF8String]);

return formatStr;

}

-(NSString *)setText:(NSString *)text{

NSString *str = text;

if (str.length==0) {

str = @"未填寫";

}

return str;

}

-(NSString *)setNumberText:(NSNumber *)number Unit:(NSString *)unit{

// [self newFloat:[number floatValue] withNumber:1];

if ([number isKindOfClass:[NSNull class]]||[number integerValue]==0) {

number = [NSNumber numberWithInteger:0];

}

NSString *str =@"";

if (unit.length==0) {

str = [NSString stringWithFormat:@"%@",[self newFloat:[number floatValue] withNumber:0]];

}else{

str = [NSString stringWithFormat:@"%@%@",[self newFloat:[number floatValue] withNumber:0],unit];

}

return str;

}

-(NSArray *)arraySorting:(NSString *)parameters Array:(NSArray *)array Methods:(BOOL)methods{//yes 是正序 ;no 是倒序

NSSortDescriptor*sorter=[[NSSortDescriptor alloc]initWithKey:parameters ascending:YES];

NSMutableArray *sortDescriptors=[[NSMutableArray alloc]initWithObjects:&sorter count:1];

NSArray *sortArray=[array sortedArrayUsingDescriptors:sortDescriptors];

NSArray* reversedArray = [[sortArray reverseObjectEnumerator] allObjects];

if (methods) {

return sortArray;

}else{

return reversedArray;


}

}




  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved