string array dictionary date math common methods

Source: Internet
Author: User
Tags mathematical constants string to file

Creates an empty string, given an assignment. NSString *astring = [[NSString alloc] init];astring = @ "This is a string!";
Use variable initialization nsstring *name = @ "ivan!"; NSString *astring = [[NSString stringwithformat:@ "My name is%@!", name]];
Determines whether a string is included to check whether the string begins with another string-(BOOL) Hasprefix: (NSString *) astring; NSString *string1 = @ "NSStringInformation.txt"; [String1 hasprefix:@ "nsstring"] = = 1? NSLog (@ "YES"): NSLog (@ "NO"); [String1 hassuffix:@ ". txt"] = = 1? NSLog (@ "YES"): NSLog (@ "NO"); Whether to include other characters nsstring *astring = [[NSString alloc] initwithstring:@ "This is a string!"]; Boolean contains = [astring rangeofstring:@ "This"].length>0; Read string from File: Initwithcontentsoffile method NSString *path = @ "Astring.text"; NSString *astring = [[NSString alloc] initwithcontentsoffile:path];   Write string to file: WriteToFile method NSString *astring = [[NSString alloc] initwithstring:@ "This is a string!"];  nsstring *path = @ "Astring.text"; [astring Writetofile:path Atomically:yes];   Comparison of two string isequaltostring methods NSString *astring01 = @ "This is a string!"; NSString *astring02 = @ "This is a string!"; BOOL result = [Astring01 isequaltostring:astring02]; The  compare method (the three values returned by comparer) NSString *ASTRING01 = @ "This is a string!"; NSString *astring02 = @ "Thisis a string! "; BOOL result = [Astring01 compare:astring02] = = Nsorderedsame;  nsorderedsame judge whether the content is the same nsstring *astring01 = @ "This is a string!"; NSString *astring02 = @ "This is a string!"; BOOL result = [Astring01 compare:astring02] = = nsorderedascending;   Do not consider case comparison string 1 nsstring *astring01 = @ "This is a string!"; NSString *astring02 = @ "This is a string!"; BOOL result = [Astring01 caseinsensitivecompare:astring02] = = Nsorderedsame;      Change the case of the string nsstring *string1 = @ "A string"; NSString *string2 = @ "String"; NSLog (@ "string1:%@", [string1 uppercasestring]);//Uppercase NSLog (@ "string2:%@", [string2 lowercasestring]);//lowercase NSLog (@ " string2:%@ ", [string2 capitalizedstring]);//The first letter size in the string search substring nsstring *string1 = @" This is a string "; NSString *string2 = @ "string"; Nsrange range = [string1 rangeofstring:string2]; int location = Range.location; int leight = Range.length; NSString *astring = [[NSString alloc] initwithstring:[nsstring stringwithformat:@ "location:%i,leight:%i", LocatIon,leight]]; Replace string nsstring *astring01 = @ "Hello China"; NSString * new = [ASTRING01 stringbyreplacingoccurrencesofstring:@ "China" withstring:@ "Beijing"];   Split string array nsstring *s = @ "A b d e F"; Nsarray *arr = [s componentsseparatedbystring:@ ""]; string arrays are stitched into strings nsarray *patharray = [Nsarray arraywithobjects:@ "Here" @ "Be", @ "Dragons", nil]; NSLog (@ "%@", [Patharray componentsjoinedbystring:@ ""]); extract substring-substringtoindex: From the beginning of the string to the specified position, but excluding the character of the position   NSString *string1 = @ "This is a string"; NSString *string2 = [string1 substringtoindex:3];  -substringfromindex: Starts at the specified position (including the character at the specified position) and includes all subsequent characters nsstring *string1 = @ "This is a string"; NSString *string2 = [string1 substringfromindex:3];  -substringwithrange://In accordance with the given position, length, arbitrarily intercept the substring from the string nsstring *string1 = @ "This is a string"; NSString *string2 = [string1 substringwithrange:nsmakerange (0, 4)];  nsmutablestring assigning capacity to strings     string = [nsmutablestring stringwithcapacity:40]; Add characters after an existing string     nsmutablestring *stRing1 = [[Nsmutablestring alloc] initwithstring:@ "This is a nsmutablestring"];     [string1 appendformat:[nsstring stringwithformat:@ ", I'll be adding some character"]];   Deletes characters in an existing string according to the given range and length  nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a Nsmutablestring "]; [String1 deletecharactersinrange:nsmakerange (0, 5)];   Inserts the given string in the specified position after the existing string  nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a Nsmutablestring "]; [String1 insertstring:@] hi! [atindex:0];   Replace existing with other strings  nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a nsmutablestring "]; [String1 setstring:@ "Hello word!"];   The original character nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ in accordance with the given range, and the string replacement Nsmutablestring "]; [String1 replacecharactersinrange:nsmakerange (0, 4) withstring:@ "that"];  

Nsarray Create array Nsarray *array = [[Nsarray alloc] Initwithobjects: @ "One", @ "one", @ "three", @ "four", nil]; The number of objects contained in the array; NSLog (@ " Self.dataarray cound:%d ", [Self.dataarray Count]); Gets the object at the specified index; NSLog (@ "Self.dataarray cound 2:%@", [Self.dataarray objectatindex:2]); Copy data from one array to another  nsmutablearray *mutablearray = [[Nsmutablearray alloc] init]; Nsarray *array = [Nsarray arraywithobjects: @ "A", @ "B", @ "C", nil];  mutablearray = [Nsmutablearray Arraywitharray:array];  array1 = [Nsarray Arraywitharray:array];   nsmutablearray *newarray = [[Nsmutablearray alloc] init];  nsarray *oldarray = [Nsarray arraywithobjects: @ "A", @ "B", @ "C", @ "D", @ "E", @ "F", @ "G", @ "H", nil];  for (int i = 0; i < [Oldarray count]; i++) {obj = [[Oldarray objectatindex:i] copy]; [NewArray Addobject:obj]; }   Fast and convenient array  nsmutablearray *newarray = [[Nsmutablearray alloc] init]; Nsarray *oldarray = [Nsarray arraywithobjects: @ "A", @ "B", @ "C", @ "D", @ "E", @ "F", @ "G", @ "H", nil];  for (id obj in oldarray) {[NewArray Addobject:obj]; }  copy and sort  nsmutablearray *newarray = [[Nsmutablearray alloc] init]; Nsarray *oldarray = [Nsarray arraywithobjects: @ "B", @ "a", @ "E", @ "D", @ "C", @ "F", @ "H", @ "G", nil];  nsenumerator *enumerator= [Oldarray objectenumerator]; ID obj; while (obj = [Enumerator nextobject]) {[NewArray addobject:obj];} [NewArray sortusingselector: @selector (compare:)];   Assigning capacity to arrays  nsmutablearray   *array = [Nsmutablearray ARRAYWITHCAPACITY:20]; Add an object at the end of the array  nsmutablearray *array = [Nsmutablearray arraywithobjects: @ "One", @ "one", @ "three", nil]; [Array addobject:@ "four"];   Delete the object at the specified index in the array  nsmutablearray *array = [Nsmutablearray arraywithobjects: @ "One", @ "one", @ "three", nil]; [Array removeobjectatindex:1];  

Array Enumeration
Front and back
Nsmutablearray *array = [Nsmutablearray arraywithobjects: @ "One", @ "one", @ "three", nil];
Nsenumerator *enumerator = [array objectenumerator];
ID thingie;
while (thingie = [Enumerator nextobject]) {NSLog (@ "thingie:%@", thingie);}
From the back forward
Nsmutablearray *array = [Nsmutablearray arraywithobjects: @ "One", @ "one", @ "three", nil];
Nsenumerator *enumerator = [array reverseobjectenumerator];
ID object;
while (object = [Enumerator Nextobject]) {NSLog (@ "object:%@", object);}


Nsdictionary Creating a Dictionary
Nsdictionary *dictionary = [[Nsdictionary alloc] initwithobjectsandkeys:@ "One", @ "1", @ "one", @ "2", @ "three", @ "3", nil];
NSString *string = [Dictionary objectforkey:@ "One"];
Nsmutabledictionary Create
Nsmutabledictionary *dictionary = [Nsmutabledictionary dictionary];
Add a dictionary
[Dictionary setobject:@ "One" forkey:@ "1"];
[Dictionary setobject:@ "," forkey:@ "2"];
[Dictionary setobject:@ "three" forkey:@ "3"];
[Dictionary setobject:@ "Four" forkey:@ "4"];
NSLog (@ "dictionary:%@", dictionary);
Delete the specified dictionary
[Dictionary removeobjectforkey:@ "3"];
NSLog (@ "dictionary:%@", dictionary);

Nsvalue (Wrapping any object)

Macro Definition # define Explanation
NSNumber can wrap a basic data type to form an object so that it can be sent messages, loaded into Nsarray, and so on.
NSNumber * Intnumber=[nsnumber numberwithint:100];
NSNumber *floatnumber=[nsnumber numberwithfloat:100.00];
int I=[intnumber Intvalue];


Common Methods of mathematics
Mathematical constants:
#define M_E 2.71828182845904523536028747135266250//E
#define M_LOG2E 1.44269504088896340735992468100189214//Log 2e
#define M_LOG10E 0.434294481903251827651128918916605082//log 10e
#define M_LN2 0.693147180559945309417232121458176568//log E2
#define M_LN10 2.30258509299404568401799145468436421//log E10
#define M_PI 3.14159265358979323846264338327950288//PI
#define M_PI_2 1.57079632679489661923132169163975144//PI/2
#define M_PI_4 0.785398163397448309615660845819875721//PI/4
#define M_1_PI 0.318309886183790671537767526745028724//1/PI
#define M_2_PI 0.636619772367581343075535053490057448//2/PI
#define M_2_SQRTPI 1.12837916709551257389615890312154517//2/sqrt (PI)
#define M_SQRT2 1.41421356237309504880168872420969808//sqrt (2)
#define M_SQRT1_2 0.707106781186547524400844362104849039//1/sqrt (2)
Common functions:
Exponential arithmetic
NSLog (@ "%.f", pow (3,2)); Result 9
Open square operation (used to calculate the distance between two points)
NSLog (@ "%.f", sqrt (16)); Result 4
Rounding NSLog (@ "Res:%.f", Ceil (3.000000000001)); Result 4
Under round NSLog (@ "Res:%.f", Floor (3.000000000001)); Result 3
Rounded NSLog (@ "Res:%.F", round (3.5)); Result 4
Minimum value NSLog (@ "Res:%.f", Fmin (5,10)); Result 5
Maximum value NSLog (@ "Res:%.f", Fmax (5,10)); Result 10
Absolute NSLog (@ "Res:%.f", Fabs (-10)); Result 10

NSDate gets the current date nsdate *date = [NSDate Date];
Comparisons between dates are available in the following ways
-(BOOL) Isequaltodate: (NSDate *) otherdate;//compared to otherdate, same return Yes
-(NSDate *) Earlierdate: (NSDate *) anotherdate;//compared with anotherdate, return the earlier date
-(NSDate *) Laterdate: (NSDate *) anotherdate;//compared with anotherdate, return the later date

Convert dates to Strings
NSLog (@ "date =%@", [Data description]);
Set Date display format
NSDateFormatter *formatter =[[[nsdateformatter alloc] init] autorelease];
[Formatter Settimestyle:nsdateformatterfullstyle]; Set several default display effects
[Formatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss a"];//set custom display effects
NSLog ([Formatter stringfromdate:date]);

Nsdata-> NSString
NSData *data;
NSString *astring = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];
Nsstring->nsdata
NSString *astring = @ "1234ABCD";
NSData *adata = [astring datausingencoding:nsutf8stringencoding];

String array Dictionary date math common method

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.