Common NSString NSDate NSArray Methods

Source: Internet
Author: User

Common NSString NSDate NSArray Methods

Method declaration and comment:

# Pragma mark string-related methods

// Convert json

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

// Judge the string

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

// Dictionary assignment

-(NSString *) dictionaryWithDic :( NSDictionary *) dicvalueForKey :( NSString *) key;

// Convert string to date

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

// Convert date to string

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

// Convert the timestamp into a string

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

// Verify the email format

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

// Verify the phone number

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

// Determines whether it is an integer.

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

// Determine whether it is a floating point:


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

// Retain a few decimal places

-(NSString *) newFloat :( float) valuewithNumber :( int) numberOfPlace;

// Value assignment in the text box

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

-(NSString *) setNumberText :( NSNumber *) numberUnit :( NSString *) unit;

-(NSArray *) arraySorting :( NSString *) parametersArray :( NSArray *) arrayMethods :( BOOL) methods; // yes indicates the forward direction; no indicates the reverse direction.


Method implementation:

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

NSString * jsonString = nil;

NSError * error;

NSData * jsonData = [NSJSONSerializationdataWithJSONObject: obj

Options: NSJSONWritingPrettyPrinted // Pass0 ifyou don't 'tcare aboutthe readabilityof thegenerated string

Error: & error];

If (! JsonData ){

NSLog (@ "Gotan error: % @", error );

} Else {

JsonString = [[NSStringalloc] initWithData: jsonDataencoding: NSUTF8StringEncoding];

}

ReturnjsonString;

}

# Pragma mark string-related methods

-(BOOL) isString :( NSString *) string {// whether the string is null

If (! [StringisKindOfClass: [NSNullclass] & string &&! [String isEqualToString: @ "(null)"] & string. length! = 0 &&! [String isEqualToString :@" "]) {

ReturnYES;

} Else {

ReturnNO;

}

}

-(NSString *) dictionaryWithDic :( NSDictionary *) dicvalueForKey :( NSString *) key {// dictionary Value

NSString * value = nil;

If (dic! = Nil & [[dicallKeys] containsObject: key]) {

Value = [dicvalueForKey: key];

} Else {

Value = @"";

}

Returnvalue;

}

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

NSDateFormatter * dateFormatter = [[NSDateFormatteralloc] init];

[DateFormattersetDateFormat: @ "yyyy-MM-ddHH: mm: ss"];

NSDate * destDate = [dateFormatterdateFromString: dateString];

ReturndestDate;

}


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

NSDateFormatter * dateFormatter = [[NSDateFormatteralloc] init];

// Zzz indicates the time zone, and zzz can be deleted, so that the returned date character does not contain the time zone information.

[DateFormattersetDateFormat: @ "MM-ddHH: mm"];

NSString * destDateString = [dateFormatter stringFromDate: date];

ReturndestDateString;

}

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

NSString * str = [value substringToIndex: 10];


NSDate * confromTimesp = [NSDatedateWithTimeIntervalSince1970: [strlongLongValue];

NSDateFormatter * formatter = [[NSDateFormatteralloc] init];

[FormattersetDateFormat: @ "MM-ddHH: mm"];

NSString * date = [formatter stringFromDate: confromTimesp];

NSDate * now = [NSDatedate];

NSString * dateNow = [formatter stringFromDate: now];

If ([[dateNowsubstringToIndex: 5] isEqualToString: [datesubstringToIndex: 5]) {

Date = @ "today ";

}

NSLog (@ "Current Time: % @ loading time % @", dateNow, date, [dateNowsubstringToIndex: 5]);

Returndate;

}

/* Email verification modified byhelensong */

-(BOOL) isValidateEmail :( NSString *) email

{

NSString * emailRegex = @ "[A-Z0-9a-z. _ % +-] + @ [A-Za-z0-9.-] + \. [A-Za-z] {2, 4 }";

NSPredicate * emailTest = [NSPredicatepredicateWithFormat: @ "SELFMATCHES % @", emailRegex];

Return [emailTest evaluateWithObject: email];

}

/* Verify the mobile phone number modified byhelensong */

-(BOOL) isValidateMobile :( NSString *) mobileNum

{

/**

* Mobile phone number

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

* Unicom: 130,131,132,152,155,156,185,186

* China Telecom: 153,180,189

*/

NSString * MOBILE = @ "^ 1 (3 [0-9] | 5 [0-35-9] | 8 [025-9]) \ d {8} $ ";

/**

10 * China Mobile: 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: 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: China Telecom

21*133,1349, 153,180,189

22 */

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

/**

25 * fixed telephones and PHS in Mainland China

26 * area code: 010,020,021,022,023,024,025,027,028,029

27 * Number: seven or eight digits

28 */

// NSString * PHS = @ "^ 0 (10 | 2 [0-5789] | \ d {3}) \ d {} $ ";

NSPredicate * regextestmobile = [NSPredicatepredicateWithFormat: @ "SELFMATCHES % @", MOBILE];

NSPredicate * regextestcm = [NSPredicatepredicateWithFormat: @ "SELFMATCHES % @", CM];

NSPredicate * regextestcu = [NSPredicatepredicateWithFormat: @ "SELFMATCHES % @", CU];

NSPredicate * regextestct = [NSPredicatepredicateWithFormat: @ "SELFMATCHES % @", CT];

If ([regextestmobileevaluateWithObject: mobileNum] = YES)

| ([RegextestcmevaluateWithObject: mobileNum] = YES)

| ([RegextestctevaluateWithObject: mobileNum] = YES)

| ([RegextestcuevaluateWithObject: mobileNum] = YES ))

{

ReturnYES;

}

Else

{

ReturnNO;

}

}


/* License plate number verification modified byhelensong */

BOOLvalidateCarNo (NSString * carNo)

{

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

NSPredicate * carTest = [NSPredicatepredicateWithFormat: @ "SELFMATCHES % @", carRegex];

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

Return [carTest evaluateWithObject: carNo];

}

// Determines whether it is an integer.

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

NSScanner * scan = [NSScannerscannerWithString: string];

Intval;

Return [scanscanInt: & val] & [scanisAtEnd];

}


// Determine whether it is a floating point:


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

NSScanner * scan = [NSScannerscannerWithString: string];

Floatval;

Return [scanscanFloat: & val] & [scanisAtEnd];

}

// Retain a few decimal places

-(NSString *) newFloat :( float) valuewithNumber :( int) numberOfPlace

{

NSString * formatStr = @ "% 0 .";

FormatStr = [formatStrstringByAppendingFormat: @ "% df", numberOfPlace];

FormatStr = [NSStringstringWithFormat: formatStr, value];

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

ReturnformatStr;

}

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

NSString * str = text;

If (str. length = 0 ){

Str = @ "not filled ";

}

Returnstr;

}

-(NSString *) setNumberText :( NSNumber *) numberUnit :( NSString *) unit {

// [SelfnewFloat: [number floatValue] withNumber: 1];

If ([numberisKindOfClass: [NSNullclass] | [numberintegerValue] = 0 ){

Number = [NSNumbernumberWithInteger: 0];

}

NSString * str = @"";

If (unit. length = 0 ){

Str = [NSStringstringWithFormat: @ "% @", [selfnewFloat: [numberfloatValue] withNumber: 0];

} Else {

Str = [NSStringstringWithFormat: @ "% @", [selfnewFloat: [numberfloatValue] withNumber: 0], unit];

}

Returnstr;

}

-(NSArray *) arraySorting :( NSString *) parametersArray :( NSArray *) arrayMethods :( BOOL) methods {// yes is the forward order; no is the reverse order

NSSortDescriptor * sorter = [[NSSortDescriptoralloc] initWithKey: parametersascending: YES];

NSMutableArray * sortDescriptors = [[NSMutableArrayalloc] initWithObjects: & sortercount: 1];

NSArray * sortArray = [array sortedArrayUsingDescriptors: sortDescriptors];

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

If (methods ){

ReturnsortArray;

} Else {

ReturnreversedArray;


}

}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.