iOS Learning Road of Objective-c (ii)--string

Source: Internet
Author: User

  1. 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"];
  2. 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];

  3. Evaluates the length of the string

     1  nsstring *string  = @ " zhou Dong Yu   " ;  2  Nsuinteger len = [string  length]; 
  4. Remove a reference to a member in a string

    1     NSString *string@ "Zhou Dong yu"; 2     0 ; 3     char character = [string characteratindex:position];
  5. 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}
  6. 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

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.