IOS string App

Source: Internet
Author: User

NSString *str1 = @ "Beijing";          NSString *str2 = @ "Beijing";          Convert all to uppercase NSLog (@ "%@", [str1 uppercasestring]);          All converted to lowercase NSLog (@ "%@", [str1 lowercasestring]);          Initial Capital NSLog (@ "%@", [str1 capitalizedstring]);          Compares two string contents whether the same BOOL b =[str1 ISEQUALTOSTRING:STR2];      Two string content comparison//nsorderedascending right > left//nsorderedsame content same//nsordereddescending left > right     Nscomparisonresult result = [str1 compare:str2];     if (result = = nsorderedascending) {NSLog (@ "right > left");     }else if (result = = Nsorderedsame) {NSLog (@ "content is the same");     }else if (result = = nsordereddescending) {NSLog (@ "left > right");     }//Ignore case to compare, return value like compare result = [str1 caseinsensitivecompare:str2];     if (result = = nsorderedascending) {NSLog (@ "right > left");     }else if (result = = Nsorderedsame) {NSLog (@ "content is the same");  }else if (result = = nsordereddescending) {NSLog (@ "left > right");   }//Determine if the string starts with the specified string [str1 hasprefix:@ "AAA"];          Determines whether the string ends with the specified string [str1 hassuffix:@ "AAA"];     Determines whether the string contains the specified string, returns the position and length nsrange range = [@ "123456" rangeofstring:@ "456"];          NSLog (@ "%@", Nsstringfromrange (range));     Reverse Search range = [@ "123456456qweasasd456" rangeofstring:@ "456" options:nsbackwardssearch];          NSLog (@ "%@", Nsstringfromrange (range));     Specify a range to search range = Nsmakerange (0, 9);     range = [@ "123456456qweasasd456" rangeofstring:@ "456" Options:nsbackwardssearch Range:range];  NSLog (@ "%@", Nsstringfromrange (range));
<pre name= "code" class= "CPP" >//string intercept nsstring *str = @ "123456789";          NSLog (@ "%@", [str substringfromindex:3]);          NSLog (@ "%@", [str substringtoindex:6]);                    NSLog (@ "%@", [Str substringwithrange:nsmakerange (3, 3)]);          Splits a string with the specified string, returning an array nsarray *array = [@ "1,2,3,4,5,6" componentsseparatedbystring:@ ","];                    NSLog (@ "%@", array);          Combines the strings in an array into a file path nsmutablearray *components = [Nsmutablearray array];          [Components addobject:@ "Users"];          [Components addobject:@ "Centralperk"];          [Components addobject:@ "Desktop"];          NSString *path = [NSString pathwithcomponents:components];  NSLog (@ "%@", Path);          Users/centralperk/desktop//divides a path into an array nsarray *array1 = [path pathcomponents];                    NSLog (@ "%@", array1);          Determines whether the absolute path (depending on whether it starts with '/') path = @ "/users/centralperk/desktop"; NSLog (@ "%i", [path Isabsolutepath]);                    Gets the last directory NSLog (@ "%@", [path lastpathcomponent]);                    Delete the last directory NSLog (@ "%@", [path stringbydeletinglastpathcomponent]);   Splicing a directory NSLog (@ "%@", [Path stringbyappendingpathcomponent:@ "AAA"]);      USERS/CENTRALPERK/DESKTOP/AAA NSLog (@ "%@", [Path stringbyappendingstring:@ "AAA"]);  USERS/CENTRALPERK/DESKTOPAAA NSLog (@ "%@", [Path stringbyappendingformat:@ "%@%@", @ "B", @ "C"]);          USERS/CENTRALPERK/DESKTOPBC//Expand name out//Get extension name, without.          NSString *str2 = @ "Users/centralperk/desktop/test.txt";          NSLog (@ "%@", [str2 pathextension]);          Add extension name, no need to bring.          NSLog (@ "%@", [str2 stringbyappendingpathextension:@ "MP3"]);                    Delete extension name, with. Piece delete NSLog (@ "%@", [str2 stringbydeletingpathextension]);          The string is converted to int double float NSString *STR3 = @ "123";          NSLog (@ "%i", [Str3 intvalue]);      NSLog (@ "%zi", [STR3 length]);              Remove the character at the specified position unichar c = [Str3 characteratindex:2];                    NSLog (@ "%c", c);          The string converted to C is const char *s = [STR3 utf8string];  NSLog (@ "%s", s);   </pre><br><br>

//*****

/--------Action string--nsstring (static string)---------------------NSString *beijing= @ "Welcome to Beijing"; String declaration nsstring *[email protected] "Beijing welcomes you a";     [NSString stringwithformat:@ "I am '%@ '", Beijing];        String formatting nsstring *zhui = [Beijing stringbyappendingstring:@ "hahaha"];                               String append bool b=[beijing Isequaltostring:log];    string comparison NSString *hh = @ "http://www.sina.com.cn";    if ([HH hasprefix:@ "http"]) {//Look for a string starting with HTTP NSLog (@ "contains http");    }else{NSLog (@ "no http");    } nsstring *ss = @ "123";                                   int a = [SS intvalue]+13;                          string to int type double DD = [SS doublevalue]+33.3;    String to double type NSLog (@ "%g", dd);//String to array NSString *zifuchuan [email protected] "one,two,three,four";    NSLog (@ "string:%@", Zifuchuan);                             Nsarray *array = [Zifuchuan componentsseparatedbystring:@ ","];//NSLog (@ "array:%@", array); LoseAll elements out of the entire array nsstring *value = [array objectatindex:0];    Remove the No. 0 element NSLog (@ "value:%@", value);//array to string NSString * zifuchuan2 = [array componentsjoinedbystring:@ ","];       NSLog (@ "zifuchuan2:%@", zifuchuan2); -substringtoindex: The position is truncated from the beginning of the string to the specified position, but does not include the character nsstring *string1 = @ "This is a string"; NSString *string2 = [string1 substringtoindex:3]; NSLog (@ "string2:%@", string2);//-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]; NSLog (@ "string2:%@", string2);//-substringwithrange: Randomly intercept substrings from a string by the given position, length, nsstring *string1 = @ "This is a string" ; NSString *string2 = [string1 substringwithrange:nsmakerange (0, 4)]; NSLog (@ "string2:%@", string2);//-------- Operation Dynamic string--nsmutablestring----------------------------------------------------nsmutablestring *mstr = [[    Nsmutablestring alloc] init];    NSString *STR1 = @ "This is a example."; Create variable String mstr = [Nsmutablestring stringwithstring:str1];    Insert character [Mstr insertstring:@ "very easy" atindex:10];    Delete some characters [mstr Deletecharactersinrange:nsmakerange (10,5)];             Find and delete nsrange substr = [MSTR rangeofstring:@ "Example"];    String lookup, you can determine if there is an if (substr.location! = nsnotfound) {[MSTR Deletecharactersinrange:substr] in the string;    }//reset string [MSTR setstring:@ "This is String AAA"];   Replacement string [MSTR Replacecharactersinrange:nsmakerange (2) withstring:@ "BBB"];    Replace the 2 string at the 15th string//Find the first one and replace nsstring *search = @ "This is";    NSString *replace = @ "An example of";    substr = [MSTR Rangeofstring:search];      if (substr.location! = nsnotfound) {[MSTR replacecharactersinrange:substr withstring:replace];    Replace the 1th encountered substr with the replace NSLog (@ "%@", MSTR);    }//Find all matches and replace search = @ "a";    replace = @ "X";    substr = [MSTR Rangeofstring:search];        while (substr.location! = nsnotfound) {[MSTR replacecharactersinrange:substr withstring:replace]; SUBSTR =[Mstr Rangeofstring:search]; } NSLog (@ "%@", MSTR); common NSString method + (ID) stringwithcontentsoffile:path Encoding:enc error: Err creates a new string and sets it to the contents of the file specified by path, uses the character encoding enc, and returns an error in err if nonzero. + (ID) stringwithcontentsofurl:url Encoding:enc error:err A new string and sets it to the contents of the URL, using the character encoding enc, and if nonzero, returns an error in Err. + (ID) string to create a new empty string.   + (ID) stringwithstring:nsstring Create a new string and set it to nsstring-(ID) initwithstring:nsstring Set the newly assigned string to nsstring-(ID) initwithcontentsoffile:path Encoding:enc error:err Set the string to the contents of the file specified by path-(ID) Initwithcontentsofurl:url Encoding;enc Error:err Sets the string to the content of the URL (nsurl*) URL, uses the character encoding enc, and returns an error in err if nonzero.   -(UNSIgned int) Lengtn Returns the number of characters in the string-(Unichar) characteratindex:i returns the Unicode character of Index i-(nsstring*) substringfromindex:i Returns the string from the beginning of I until the end-(nsstring*) substringtoindex:i returns the substring from the starting position of the string to the index i-(Nscomparator *) Caseinsensitivecompare: NSString compare two strings, ignore case-(nscomparator *) compare:nsstring compare two strings-(BOOL) hasprefix:nsstring Tests whether a string starts with NSString-(BOOL) isequaltostring:nsstring tests whether two strings are equal. -(nsstring*) capitalizedstring return each word firstUppercase string (the remaining letters of each word are converted to lowercase)-(NSString *) lowercasestring returns a string converted to lowercase-(NSString *) uppercasestring returns a string that is caught in uppercase-(const char *) utf8string returns a string converted to a UTF8 string-(double) Doublevalue returns a string converted to a Nsinteger integer-(int) intvalue Returns a string converted to an integer nsmutablestring character method + (ID) stringwithcapacity:size creates a string that initially contains the size of the character-(ID) initwithcapacity:size Initializes the string with the initial capacity of size-(void) setstring:nsstring sets the string to nsstring-(void) appendstring:nsstring appends the nsstring-to the end of the recipient ( void) Deletecharactersinrange:range Delete the character in the specified range-(void) insertstring:nsstring atindex:i insert nsstring-(void) with index i as the starting position Replacecharactersinrange:range withstring:nsstring uses NSString to replace the character specified by range-(void) replaceoccurrencesof String: NSString withstring:nsstring2 options:opts Range:range replaces all opts with NSSTRING2 in the specified range, based on the option NSString. Options can include Nsbackwardssearch (search from the end of the range), Nsan hot spot search (NSString must match the start of the range), Nsliteralsearch

IOS string App

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.