Common ios operations

Source: Internet
Author: User

1. determine whether an element BOOLisValue = [keyArray containsObject: Q "aaa"] exists in the array. 2. Separate the strings by commas and save them to the array: NSArray * keyArray = [[NSArray alloc] init]; keyArray = [@ "winter melon, watermelon, pumpkin, bitter melon, silk melon" componentsSeparatedByString: @ ","]; 3. Extract the array and splice it into a string separated by commas: NSString * n = [keyArray componentsJoinedByString: @ ","]; 4. NSMutableArray is converted to NSArray * phoneA = [[NSArray alloc] init]; NSMutableArray * phoneArrayss = [[NSMutableArray alloc] init]; phoneA = [phon EArrayssmutableCopy]; 5. Obtain the local time (in case of HH in 24-hour format) NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formattersetDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; NSString * timeStr = [formatter stringFromDate: [NSDate date]; 6. Remove spaces in nsstring NSCharacterSet * whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; NSString * username = [mUsernameField stringValue]; username = [usernamestringByTrimmingChar ActersInSet: whitespace]; 7. Where to judge: IsNetworking * IsNetWork = [[IsNetworking alloc] init]; BOOL isNets = [IsNetWork isNetworkReachable]; if (isNets = NO) {// No network} else {// network} 8. append character: NSMutableString * string = [[NSMutableString alloc] init]; string = @ "hello"; [stringappendFormat: @ "China"]; 9. String replacement: replace all <in info with # NSString * stroneIntro = [info stringByReplacingOccurrencesOfString: @ "<" withString: @ "#"]; 10. String comparison: BoolIsTrue = [@ "Nob" isEqualToString: @ "Mob"] 11. a case-insensitive comparison String NSString * astring01 = @ "this is a String! "; NSString * astring02 = @" This is a String! "; BOOLresult = [astring01 caseInsensitiveCompare: astring02] = NSOrderedSame; 12. NSString * string1 = @" A String "; NSString * string2 = @" String "; NSLog (@ "string1: % @", [string1uppercaseString]); // upper case NSLog (@ "string2: % @", [string2lowercaseString]); // lowercase NSLog (@ "string2: % @", [string2capitalizedString]); // initial character size 13. Search for the substring NSString * string1 = @ "This is a string"; NSString * string2 = @ "string"; nsanger Ange = [string1 rangeOfString: string2]; intlocation = range. location; intleight = range. length; NSString * astring = [[NSString alloc] initWithString: [NSStringstringWithFormat: @ "Location: % I, Leight: % I", location, leight]; NSLog (@ "astring: % @", astring); [astringrelease]; 14. Extract the substring //-substringToIndex: extract the substring from the beginning of the string to the specified position, but does not include the NSString * string1 = @ "This is a string"; NSString * string2 = [string1 substr IngToIndex: 3]; NSLog (@ "string2: % @", string2); //-substringFromIndex: starts at the specified position (including characters at the specified position ), and all subsequent characters NSString * string1 = @ "This is a string"; NSString * string2 = [string1substringFromIndex: 3]; NSLog (@ "string2: % @", string2); //-substringWithRange: // truncate the substring NSString * string1 = @ "This is a string" randomly according to the given position and length "; NSString * string2 = [string1 substringWithRange: NSMakeRange (0, 4)]; NSLog (@ "string2: % @", string2 ); 15. Determine whether the string contains another string (prefix, suffix) // 01: Check whether the string starts with another string-(BOOL) hasPrefix: (NSString *) aString; NSString * String1 = @ "NSStringInformation.txt"; [String1hasPrefix: @ "NSString"] = 1? NSLog (@ "YES"): NSLog (@ "NO"); [String1hasSuffix: @ ". txt"] = 1? NSLog (@ "YES"): NSLog (@ "NO ");

Related Article

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.