Common uses of NSString in iOS

Source: Internet
Author: User
Tags strcmp

Common usage of IOS nsstring//1, creating constant strings. NSString *astring = @ "This is a string!"; 2, create an empty string, give the assignment. NSString *astring = [[NSString alloc] init]; Astring = @ "This is a string!";  /3, in the above method, the lifting speed: Initwithstring method NSString *astring = [[NSString alloc] initwithstring:@ "This is a string!"]; 4. Create a string with standard C: Initwithcstring method Char *cstring = "This is a string!";  NSString *astring = [[NSString alloc] initwithcstring:cstring]; 5. Create a formatted string: placeholder (consisting of a% plus one character) int i = 1; int j = 2; NSString *astring = [[NSString alloc] initwithstring:[nsstring stringwithformat:@ "%d.this is%i string!", I,j]]; NSLog (@ "astring:%@", astring); 6, create temporary string nsstring *astring;  astring = [NSString stringwithcstring: "This is a temporary string"]; 7. Create string from file NSString *path = [[Nsbundlemainbundle] pathforresource:@ "astring" oftype:@ "txt"]; NSString *astring = [[NSString alloc] initwithcontentsoffile:path]; 8. Create string with string and write to file nsstring *astring = [[NSString alloc] initwithstring:@ "This is a string!"]; NSLog (@ "astring:%@", astring); NSString *path = @ "Astring.text"; [astring Writetofile:path Atomically:yes]; Note: This path is only indicative, the true path is not so//9, compared with C: strcmp function char string1[] = "string!"; Char string2[] = "string!"; if (strcmp (string1, string2) = = 0) {NSLog (@ "1");}//10, isequaltostring method NSString *astring01 = @ "This is a string!" ; NSString *astring02 = @ "This is a string!"; BOOL result = [Astring01 isequaltostring:astring02]; NSLog (@ "result:%d", result),//11, compare method (three values returned by comparer)//nsstring *astring01 = @ "This is a string!";     NSString *astring02 = @ "This is a string!";    BOOL result = [Astring01 compare:astring02] = = Nsorderedsame;     Nsorderedsame judge whether the content is the same NSLog (@ "result:%d", result);    BOOL result = [Astring01 compare:astring02] = = nsorderedascending; Nsorderedascending determines the size of two object values (in alphabetical order, ASTRING02 is greater than ASTRING01) NSLog (@ "result:%d", result);    BOOL result = [Astring01 compare:astring02] = = nsordereddescending;      Nsordereddescending determines the size of two object values (in alphabetical order, ASTRING02 is less than ASTRING01) NSLog (@ "result:%d", result); 12, do not consider the case comparison string//1.nsstring *astring01 = @ "This is a string!"; NSString *astring02 = @ "This is a string!";    BOOL result = [Astring01 caseinsensitivecompare:astring02] = = Nsorderedsame; Nsordereddescending determines the size of two object values (in alphabetical order, ASTRING02 is less than ASTRING01) NSLog (@ "result:%d", result); 2.NSString *astring01 = @ "This is a string!"; NSString *astring02 = @ "This is a string!"; BOOL result = [Astring01 compare:astring02 options:nscaseinsensitivesearch |    Nsnumericsearch] = = Nsorderedsame; Nscaseinsensitivesearch: Case-insensitive comparison nsliteralsearch: Make a full comparison, case-sensitive nsnumericsearch: Compares the number of characters in a string, not the character value.  NSLog (@ "result:%d", result);  13. Output uppercase or lowercase string nsstring *string1 = @ "A string";  NSString *string2 = @ "String"; NSLog (@ "string1:%@", [string1 uppercasestring]);//Uppercase NSLog (@ "string2:%@", [string2 lowercasestring]);//lowercase NSLog (@ " string2:%@ ", [string2 capitalizedstring]);//first letter size//14,-rangeofstring://Find string somewhere contains other strings 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]]; NSLog (@ "astring:%@", astring); [Astring release]; 15,-substringtoindex: From the beginning of the string is truncated to the specified position, but does not include the position of the character nsstring *string1 = @ "This is a string"; NSString *string2 = [string1 substringtoindex:3]; NSLog (@ "string2:%@", string2); 16,-substringfromindex: Start at the specified position (including the character at the specified position), and include all subsequent characters nsstring *string1 = @ "This is a string"; NSString *string2 = [string1 substringfromindex:3]; NSLog (@ "string2:%@", string2); 17,-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)]; NSLog (@ "string2:%@", string2); 18,-stringwithcapacity://According to fixed length to generate an empty string nsmutablestring *string; String = [nsmutablestring stringwithcapacity:40]; 19.-appendstring:and-appendformat://Connect one string to the end of another string nsmutablestring *string1 = [[NsmutablestRing Alloc] initwithstring:@ "This is a nsmutablestring"]; [String1 appendstring:@], I'll be adding some character "]; [String1 appendformat:[nsstring stringwithformat:@ ", I'll be adding some character"]]; NSLog (@ "string1:%@", String1);//20,-insertstring:atindex://Insert string at specified location nsmutablestring *string1 = [[nsmutablestring Alloc] initwithstring:@ "This is a nsmutablestring"]; [String1 insertstring:@] hi! [atindex:0]; NSLog (@ "string1:%@", String1); 21,-setstring:nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a nsmutablestring"]; [String1 setstring:@ "Hello word!"]; NSLog (@ "string1:%@", String1); 22,-replacecharactersinrange:withstring://Replace the string with the specified string in the specified position, length of the string nsmutablestring *string1 = [[nsmutablestring Alloc] initwithstring:@ "This is a nsmutablestring"]; [String1 replacecharactersinrange:nsmakerange (0, 4) withstring:@ "that"]; NSLog (@ "string1:%@", String1); 23,-hasprefix://Check whether the string begins with another string nsstring *string1 = @ "NSStringInformation.txt"; [String1 Hasprefix:@ "NSString"] = = 1? NSLog (@ "YES"): NSLog (@ "NO");  [String1 hassuffix:@ ". txt"] = = 1? NSLog (@ "YES"): NSLog (@ "NO"); 24, extension path nsstring *path = @ "~/nsdata.txt"; NSString *absolutepath = [Path Stringbyexpandingtildeinpath]; NSLog (@ "absolutepath:%@", Absolutepath); NSLog (@ "path:%@", [Absolutepath Stringbyabbreviatingwithtildeinpath]);//25, file extension nsstring *path = @ "~/nsdata.txt"; NSLog (@ "extension:%@", [Path pathextension]);

 

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.