Creation of NSString objects
1 //Creating immutable Strings2NSString *string1 =@"bei Jing Huan Ying Nin";3 #pragmaThe Mark object method creates a string4 //initializes the current string with another string with the same address as the pointer5NSString *string2 =[[NSString alloc] initwithstring:string1];6 //converting other data types to string types7NSString *string3 = [[NSString alloc] Initwithformat:@"Age was%d, name is%@", at,@"DYK"];8 #pragmaThe Mark class method creates a string9 //the method corresponds to the Initwithstring method .TenNSString *string4 =[NSString stringwithstring:string1]; One //the method corresponds to the Initwithformat method . ANSString *string5 = [NSString stringWithFormat:@"Age was%d, name is%@", at,@"DYK"];
Concatenation of strings
1NSString *string1 =@"DYK";2NSString *string2 =@"Shuai";3 //-(NSString *) stringbyappendingstring: (NSString *) astring;4 //function: Concatenation of two strings5 //two concatenation of the string has not changed, the return value is the concatenation of the string6NSString *string3 =[string1 stringbyappendingstring:string2];7NSString *string4 = [NSString stringWithFormat:@"%@%@", string1, string2];
-
Evaluates the length of the string
1 nsstring *string = @ " zhou Dong Yu " ; 2 Nsuinteger len = [string length];
Remove a reference to a member in a string
1 NSString *string@ "Zhou Dong yu"; 2 0 ; 3 char character = [string characteratindex:position];
Comparison of strings
1NSString *string1 =@"Chen Chao";2NSString *string2 =@"CHAN CHAO";3 #if04 //-(BOOL) isequaltostring: (NSString *) astring;5 //function: Compare two strings are identical6 //Bool:yes (True), NO (false)7BOOL BL =[string1 isequaltostring:string2];8 if(BL) {9NSLog (@"string1 = = string2");Ten}Else{ OneNSLog (@"string1! = string2"); A } - #endif - the #if0 - //-(Nscomparisonresult) Compare: (NSNumber *) Decimalnumber; - //function: Compares two strings (not ignoring case) - //nscomparisonresult:nsorderedascending (Ascending) + //Nsorderedsame (same) - //nsordereddescending (Descending) +Nscomparisonresult result =[string1 compare:string2]; A if(Result = =nsordereddescending) { atNSLog (@"String1 < string2"); -}Else if(Result = =nsorderedascending) { -NSLog (@"string1 > string2"); -}Else { -NSLog (@"string1 = = string2"); - } in #endif - //-(Nscomparisonresult) Caseinsensitivecompare: (NSString *) string; to //function: Compares two strings for the same (ignoring case) +Nscomparisonresult result =[string1 caseinsensitivecompare:string2]; - if(Result = =nsordereddescending) { theNSLog (@"String1 < string2"); *}Else if(Result = =nsorderedascending) { $NSLog (@"string1 > string2");Panax Notoginseng}Else { -NSLog (@"string1 = = string2"); the}
Find the location of a substring in a string
1NSString *string1 =@"Chen Chao Zui shuai";2NSString *string2 =@"Zui";3 4 //-(Nsrange) rangeofstring: (NSString *) astring;5 //function: Find the location of a substring in a string6 //typedef struct _NSRANGE {7 //Nsuinteger location; Start position8 //Nsuinteger length; Length starting from the starting position9 //} nsrange;TenNsrange range =[string1 rangeofstring:string2]; One if(Range.location = =nsnotfound) { ANSLog (@"not found"); - } -NSLog (@"Location was%lu, length is%lu", Range.location, range.length);
iOS Learning Road of Objective-c (ii)--string