Several common usages of nssrting

Source: Internet
Author: User
Tags float double string to file

1. Create NSString string nsstring The biggest difference with char* is that NSString is a objective object, and char* is a byte array.      The @+ "string" is the standard usage of objective-c NSString string constants, char* created without adding @-(void) viewdidload [Super Viewdidload];      Classic string Assignment NSString *str0 = @ "My name is justcoding!"; String formatting merges include//nsstring* type int type char* type NSString *str1 = [NSString stringwithformat:@ "My Name:%@ my age:%d my e-mail:%      S ", @" justcoding "," [email protected] ";      String assignment parameters can only be written in one string and the first is much like nsstring *str2 = [NSString stringwithstring:@ "I am a string"];      The string is converted to the utf-8 format parameter to char* type nsstring *STR3 = [NSString stringwithutf8string: "String conversion utf-8 format"];      string merge int i = 100;      Char*c = "Xuanyusong";      NSString *temp = @ "I am a temporary string";      The base of the string temp continues to add int i with char* c to form a new string nsstring *STR4 = [Temp stringbyappendingformat:@ "integer:%d character type:%s", I,c];       On the basis of the string temp continue to add temp and compose a new string nsstring *STR5 = [temp stringbyappendingstring:temp];      String output NSLog (@ "STR0 =%@", STR0); NSLog (@ "sTR1 =%@ ", str1);      NSLog (@ "str2 =%@", str2);      NSLog (@ "STR3 =%@", STR3);      NSLog (@ "STR4 =%@", STR4);  NSLog (@ "STR5 =%@", STR5);        -(void) viewdidload{[Super Viewdidload];        Classic string Assignment NSString *str0 = @ "My name is justcoding!"; String formatting merge includes//nsstring* type int type char* type NSString *str1 = [NSString stringwithformat:@ "My Name:%@ my age:%d my mailbox:%s", @        "Justcoding", "[email protected]";       String assignment parameters can only be written in one string and the first is much like nsstring *str2 = [NSString stringwithstring:@ "I am a string"];            The string is converted to the utf-8 format parameter to char* type nsstring *STR3 = [NSString stringwithutf8string: "String conversion utf-8 format"];    string merge int i = 100;    Char*c = "Xuanyusong";        NSString *temp = @ "I am a temporary string";        The base of the string temp continues to add int i with char* c to form a new string nsstring *STR4 = [Temp stringbyappendingformat:@ "integer:%d character type:%s", I,c];         On the basis of the string temp continue to add temp and compose a new string nsstring *STR5 = [temp stringbyappendingstring:temp];    String output NSLog (@ "STR0 =%@", STR0); NSLog (@ "StR1 =%@ ", str1);    NSLog (@ "str2 =%@", str2);    NSLog (@ "STR3 =%@", STR3);    NSLog (@ "STR4 =%@", STR4);    NSLog (@ "STR5 =%@", STR5); }2. String traversal each string is actually made up of a number of char characters, and the traversal of the string actually extracts every character in the string.      -(void) viewdidload [Super Viewdidload];      Classic string Assignment NSString *str = @ "Yusongmomo";      The length of the string int count = [str length];      NSLog (@ "The length of the string is%d", count);          Iterate through each character in a string for (int i =0; i < count; i++) char c = [Str characteratindex:i];  NSLog (@ "string%d bits is%c", i,c);        -(void) viewdidload{[Super Viewdidload];        Classic string Assignment NSString *str = @ "Yusongmomo";        The length of the string int count = [str length];        NSLog (@ "The length of the string is%d", count);        Iterate through each character in a string for (int i =0; i < count; i++) {char c = [Str characteratindex:i];    NSLog (@ "string%d bits is%c", i,c); }} 3. Comparison of strings isequaltostring comparison strings are exactly equal, and the case does not match exactly.      Hasprefixe matches the string header hasuffix the tail of the matched string-(void) viewdidload [Super Viewdidload]; NSString *STR0 = @"Justcoding";      NSString *str1 = @ "justcoding";      strings are exactly equal compared if ([Str0 isequaltostring:str1]) NSLog (@ "string is exactly equal");      The string is compared at the beginning with the if ([Str0 hasprefix:@ "just"]) NSLog (@ "string str0 starts with just");  The string compares the if ([str1 hassuffix:@ "coding"]) NSLog (@ "str1 string ends with coding") at the end;            -(void) viewdidload{[Super Viewdidload];    NSString *str0 = @ "justcoding";            NSString *str1 = @ "justcoding";    strings are exactly equal compared if ([Str0 isequaltostring:str1]) {NSLog (@ "string is exactly equal");    }//String to begin comparing if ([Str0 hasprefix:@ "just"]) {NSLog (@ "string str0 starts with just");    }//String with the end comparison if ([str1 hassuffix:@ "coding"]) {NSLog (@ "str1 string ends with coding");  }}//isequaltostring method NSString *astring01 = @ "This is a string!";  NSString *astring02 = @ "This is a string!";  BOOL result = [Astring01 isequaltostring:astring02];  NSLog (@ "result:%d", result); Isequaltostring method NSString *astring01 = @ "This is a string!"; NSString *astring02= @ "This is a string!"; BOOL result = [Astring01 isequaltostring:astring02]; NSLog (@ "result:%d", result);  The Compare method (the three values returned by comparer) NSString *ASTRING01 = @ "This is a string!";      NSString *astring02 = @ "This is a string!";      BOOL result = [Astring01 compare:astring02] = = Nsorderedsame;      NSLog (@ "result:%d", result); Nsorderedsame determines whether the content is the same//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;    NSLog (@ "result:%d", result);  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;  NSLog (@ "result:%d", result); Nsorderedascending determines the size of two object values (in alphabetical order, astring02 greater than ASTRING01 is true) nsstring *astring01 = @ "This is a string!"; NSString *astring02 = @ "This is a string!"; BOOL result = [Astring01 compare:astring02] = = Nsorderedascending; NSLog (@ "result:%d", result),//nsorderedascending determines the size of two object values (in alphabetical order, ASTRING02 is greater than ASTRING01 is true) nsstring *astring01 = @  "This is a string!";  NSString *astring02 = @ "This is a string!";      BOOL result = [Astring01 compare:astring02] = = nsordereddescending;      NSLog (@ "result:%d", result); Nsordereddescending determines the size of two object values (in alphabetical order, ASTRING02 is less than astring01 true) nsstring *astring01 = @ "This is a string!"; NSString *astring02 = @ "This is a string!";    BOOL result = [Astring01 compare:astring02] = = nsordereddescending;    NSLog (@ "result:%d", result); Nsordereddescending determines the size of two object values (in alphabetical order, ASTRING02 is less than astring01 true)//does not consider case comparison string 1 nsstring *astring01 = @ "This is a stri  Ng! ";  NSString *astring02 = @ "This is a string!";      BOOL result = [Astring01 caseinsensitivecompare:astring02] = = Nsorderedsame;      NSLog (@ "result:%d", result); Nsordereddescending determines the size of two object values (in alphabetical order, ASTRING02 is less than astring01 true)//does not consider case comparison string 1NSString *astring01 = @ "This is a String! "; NSString *astring02 =@ "This is a string!";    BOOL result = [Astring01 caseinsensitivecompare:astring02] = = Nsorderedsame;    NSLog (@ "result:%d", result); Nsordereddescending determines the size of two object values (in alphabetical order, ASTRING02 is less than astring01 true)//does not consider case comparison string 2 nsstring *astring01 = @ "This is a  String! ";  NSString *astring02 = @ "This is a string!"; BOOL result = [Astring01 compare:astring02 options:nscaseinsensitivesearch |      Nsnumericsearch] = = Nsorderedsame;      NSLog (@ "result:%d", result);  Nscaseinsensitivesearch: Case-insensitive comparison nsliteralsearch: Make a full comparison, case-sensitive nsnumericsearch: Compares the number of characters in a string, not the character value. Do not consider case comparison string 2NSString *astring01 = @ "This is a string!"; NSString *astring02 = @ "This is a string!"; BOOL result = [Astring01 compare:astring02 options:nscaseinsensitivesearch |    Nsnumericsearch] = = Nsorderedsame;    NSLog (@ "result:%d", result); Nscaseinsensitivesearch: Case-insensitive comparison nsliteralsearch: Make a full comparison, case-sensitive nsnumericsearch: Compares the number of characters in a string, not the character value. The method of judging can use the Caseinsensitivecompare in the NSString Category: 1, 0 or 1 to determine the sort relationship between two strings, and its program is as follows. NSString *string = @ "0";   Nscomparisonresult result = [string caseinsensitivecompare:@ "A"];   Switch (Result) {case Nsorderedascending:nslog (@ "ascending");   Break   Case Nsorderedsame:nslog (@ "ignores strings of the same casing");   Break   Case Nsordereddescending:nslog (@ "Power down");   Break   Default:nslog (@ "cannot be determined");  Break NSString *string = @ "0"; Nscomparisonresult result = [string caseinsensitivecompare:@ "A"]; Switch (Result) {case Nsorderedascending:nslog (@ "ascending"), Break Nsorderedsame:nslog (@ "Ignore the same casing string"); Dereddescending:nslog (@ "Power down"); Break Default:nslog (@ "cannot be determined"); Break } 4.  File string operation (read/write)//read string from File: Initwithcontentsoffile method NSString *path = @ "Astring.text";  NSString *astring = [[NSString alloc] initwithcontentsoffile:path];  NSLog (@ "astring:%@", astring); [Astring release];  Astring=nil; Read string from File: Initwithcontentsoffile method NSString *path = @ "Astring.text"; NSString *astring = [[NSString alloc] initwithcontentsoffile:path]; NSLog (@ "astring:%@", astring); [Astring release]; Astring=niL  Write string to File: WriteToFile method NSString *astring = [[NSString alloc] initwithstring:@ "This is a string!"];  NSLog (@ "astring:%@", astring);      NSString *path = @ "Astring.text";  [astring Writetofile:path Atomically:yes];  [Astring release];  Astring=nil; Write string to File: WriteToFile method NSString *astring = [[NSString alloc] initwithstring:@ "This is a string!"]; NSLog (@ "astring:%@", astring);    NSString *path = @ "Astring.text"; [astring Writetofile:path Atomically:yes];  [Astring release]; Astring=nil; * iOS5 does not support release iOS app development: What is ARC?  Extension path NSString *path = @ "~/nsdata.txt";  NSString *absolutepath = [Path Stringbyexpandingtildeinpath];  NSLog (@ "absolutepath:%@", Absolutepath);  NSLog (@ "path:%@", [Absolutepath Stringbyabbreviatingwithtildeinpath]); Extension path NSString *path = @ "~/nsdata.txt"; NSString *absolutepath = [Path Stringbyexpandingtildeinpath]; NSLog (@ "absolutepath:%@", Absolutepath); NSLog (@ "path:%@", [Absolutepath Stringbyabbreviatingwithtildeinpath]);//File extension nsstring *path = @ "~/nsdata.txt";  NSLog (@ "extension:%@", [Path pathextension]); File extension NSString *path = @ "~/nsdata.txt"; NSLog (@ "extension:%@", [Path pathextension]);  5. String interception and capitalization//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]);//first letter size//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]);//first Letter size Java code-(void) viewdidload [Super Viewdidload];      NSString *str0 = @ "Chinese name is Xuanyusong";      Intercepts the starting point of the string to the content between index 4 NSString * to = [STR0 substringtoindex:4];      NSLog (@ "to =%@", to);      The Intercept character index is 2 to the end of the content nsstring * from = [STR0 substringfromindex:2];      NSLog (@ "from =%@", from); Sets the range of intercept strings//starting with the second bit, the length is 10 nsrange rang = NSmakerange (2, 10);      NSString * Strrang = [Str0 Substringwithrange:rang];      NSLog (@ "rang =%@", Strrang);      Set the first letter of the string NSLog (@ "STR0 Capital:%@", [Str0 capitalizedstring]);      Sets the entire contents of the string to uppercase NSLog (@ "STR0 uppercase:%@", [Str0 uppercasestring]);  Set character full contents to lowercase NSLog (@ "Str0 lowercase:%@", [Str0 lowercasestring]);            -(void) viewdidload{[Super Viewdidload];        NSString *str0 = @ "Chinese name is Xuanyusong";        Intercepts the starting point of the string to the content between index 4 NSString * to = [STR0 substringtoindex:4];        NSLog (@ "to =%@", to);        The Intercept character index is 2 to the end of the content nsstring * from = [STR0 substringfromindex:2];            NSLog (@ "from =%@", from);    Sets the range of intercept strings//starting with the second bit, the length is 10 nsrange rang = Nsmakerange (2, 10);    NSString * Strrang = [Str0 Substringwithrange:rang];        NSLog (@ "rang =%@", Strrang);    Set the first letter of the string NSLog (@ "STR0 Capital:%@", [Str0 capitalizedstring]);    Sets the entire contents of the string to uppercase NSLog (@ "STR0 uppercase:%@", [Str0 uppercasestring]); Set character all contents to lowercase NSLog (@ "Str0 lowercase:%@", [str0 lowercaseString]);      6. Search string with replacement string-(void) viewdidload [Super Viewdidload];      NSString *str0 = @ "Chinese name is Xuanyusong";      NSString *temp = @ "is";      Nsrange rang = [Str0 rangeofstring:temp];      NSLog (@ "The string searched at the starting point in Str0 of index%d", rang.location);      NSLog (@ "The string searched at the end of the STR0 in the index is%d", rang.location + rang.length);       Replace the string in the search with a new string nsstring *str = [str0 stringbyreplacingcharactersinrange:rang withstring:@ "wow ka ka Kaka"];      NSLog (@ "replacement string is%@", str);       Replace All "" in the string with * str = [STR0 stringbyreplacingoccurrencesofstring: @ "" withstring:@ "@"];  NSLog (@ "replacement string is%@", str);            -(void) viewdidload{[Super Viewdidload];    NSString *str0 = @ "Chinese name is Xuanyusong";    NSString *temp = @ "is";        Nsrange rang = [Str0 rangeofstring:temp];    NSLog (@ "The string searched at the starting point in Str0 of index%d", rang.location);        NSLog (@ "The string searched at the end of the STR0 in the index is%d", rang.location + rang.length); Replaces a string in a search with a new string nsstring *str = [Str0 stringbyreplacingcharactersinRange:rang withstring:@ "wow ka ka kaka ka"];        NSLog (@ "replacement string is%@", str);     Replace All "" in the string with * str = [STR0 stringbyreplacingoccurrencesofstring: @ "" withstring:@ "@"];        NSLog (@ "replacement string is%@", str); Expand: Use the following method to replace the whole string and set the replacement area. Stringbyreplacingoccurrencesofstring: (NSString *) withstring: (NSString *) options: (nsstringcompareoptions) Range: ( Nsrange) 7. String trailing add uses Alloc to create a string object in memory behind which you can dynamically manipulate the string, modify and add, and so on. AppendString method: Adds a string to the tail of the string. AppendFormat method: To add multiple types of strings to the end of a string, you can add any number of strings to the type.      -(void) viewdidload [Super Viewdidload];      nsmutablestring * str = [[Nsmutablestring alloc] init];      Add a normal string [str appendstring:@ "AAA"];      Add string integer character type [str appendformat:@ "My Name:%@ my age:%d my mailbox:%s", @ "justcoding", "[email protected]"];  NSLog (@ "str =%@", str);           -(void) viewdidload{[Super Viewdidload];        nsmutablestring * str = [[Nsmutablestring alloc] init];        Add a normal string [str appendstring:@ "AAA"]; Add string integer character type [str appendformat:@] My name:%@ my Age:%d my mailbox:%s ", @" justcoding "," [email protected] ";    NSLog (@ "str =%@", str); }8. Removing elements from a string stringwithstring method: Used to create a string initialization assignment Rangeofstring method: Passing in a string returns a range within that string can also be written nsmakerange (0, 3) Meaning range in string 0 bits to 3rd bit deletecharactersinrange: Delete string argument is range is the scope of the deletion.      -(void) viewdidload [Super Viewdidload];      Create string nsmutablestring *str = [nsmutablestring stringwithstring:@ "justcoding is best!"];          Delete the character in the string containing "justcoding" [str deletecharactersinrange: [str rangeofstring: @ "justcoding"];  NSLog (@ "str =%@", str);        -(void) viewdidload{[Super Viewdidload];        Create string nsmutablestring *str = [nsmutablestring stringwithstring:@ "justcoding is best!"];            Delete the character in the string containing "justcoding" [str deletecharactersinrange: [str rangeofstring: @ "justcoding"];    NSLog (@ "str =%@", str); } 9. String insertion Stringwithstring method: Used to create a string initialization assignment Insertstring method: The first parameter inserts the string object, and the second parameter is inserted in the position.      -(void) viewdidload [Super Viewdidload]; Create string nsmutablestring *str = [NsmutaBlestring stringwithstring:@ "Justcoding is"];      Insert string in str 10th [str insertstring:@ "Best man" atindex:10];  NSLog (@ "str =%@", str);        -(void) viewdidload{[Super Viewdidload];        Create string nsmutablestring *str = [nsmutablestring stringwithstring:@ "justcoding is"];        Insert string in str 10th [str insertstring:@ "Best man" atindex:10];    NSLog (@ "str =%@", str); } 10.      String copy-(void) viewdidload [Super Viewdidload];      Create string nsmutablestring *str1 = [nsmutablestring stringwithstring: @ "string 1"];      Nsmutablestring *str2;      String Assignment str2 = str1;      [str2 appendString: @ "and string 2];      NSLog (@ "str1 =%@", str1);      NSLog (@ "str2 =%@", str2);        -(void) viewdidload{[Super Viewdidload];    Create string nsmutablestring *str1 = [nsmutablestring stringwithstring: @ "string 1"];        Nsmutablestring *str2;        String Assignment str2 = str1;        [str2 appendString: @ "and string 2];        NSLog (@ "str1 =%@", str1);    NSLog (@ "str2 =%@", str2); } Why did the STR1 data change after adding data to str2? This is the charm of the pointer, because we operate a pointer, str2 = STR1 means that two pointers point to a piece of memory, then STR2 point to the memory content changes after str1 of course changed. 11. String with the specified type conversion if the parameters of the conversion is illegal, it will not throw an exception, like in Chinese to go to the integer type. No error but the conversion result is 0, the default value.      -(void) viewdidload [Super Viewdidload];      string-to-integer nsstring *str0 = @ "1121";      NSString *str0 = @ "China";      The string is strongly converted to int i = [Str0 intvalue];       NSLog (@ "converted:%i", i);      String goto interger NSString *str1 = @ "1985";      NSString *str1 = @ "China";      Turn the string strongly into Interger Nsinteger II = [str1 integervalue];       NSLog (@ "converted:%i", ii);      The string goes double nsstring *str2 = @ "3.145926";      NSString *str2 = @ "China";      Turn the string strongly into double double d = [str2 doublevalue];       NSLog (@ "converted:%f", D);       String to float NSString *STR3 = @ "3.145926";      NSString *STR3 = @ "China";      Turn the string strongly into a float double f = [Str3 floatvalue];   NSLog (@ "converted:%f", f);        -(void) viewdidload{[Super Viewdidload];    string-to-integer nsstring *str0 = @ "1121"; NSString *str0 = @ "China";        The string is strongly converted to int i = [Str0 intvalue];         NSLog (@ "converted:%i", i);    String goto interger NSString *str1 = @ "1985";        NSString *str1 = @ "China";        Turn the string strongly into Interger Nsinteger II = [str1 integervalue];                 NSLog (@ "converted:%i", ii);    The string goes double nsstring *str2 = @ "3.145926";        NSString *str2 = @ "China";        Turn the string strongly into double double d = [str2 doublevalue];         NSLog (@ "converted:%f", D);     String to float NSString *STR3 = @ "3.145926";    NSString *STR3 = @ "China";        Turn the string strongly into a float double f = [Str3 floatvalue];     NSLog (@ "converted:%f", f); }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Several common usages of nssrting

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.