Foundation framework--nsstring Class string common operations

Source: Internet
Author: User

Comparative content bool isequal= "string1 isequaltostring:string2];
Compare pointer addresses (STRING1==STRING2)
Compare String size ascending order

Nscomparisonresult result =[string10 caseinsensitivecompare:string11];


To find the string lengthNSlog (@ "%d", [string11 length]);
String ConversionsNSLog (@ "upper:%@", [string11 uppercasestring]); uppercaseNSLog (@ "lower:%@", [string11 lowercasestring]); lowercaseNSLog (@ "capital:%@", [string11 capitalizedstring]); Capitalize first letter
converting basic data Typesnsstring *[email protected] "3,14";float PPI=[STRING13 Floatvalue];
String Conversion Arraynsstring* string14 [email protected] "ABC BCD XYZ";nsarray*array=[string14 componentsseparatedbystring:@ ""];(quotation marks are separated by a space)NSLog (@ "array:%@", array);
Intercept StringNsstring*string1=[string15 Substringtoindex:2]; does not contain a second (to that)nsstring*string2=[string15 Substringfromindex:2:]; contains the second bit (starting from that bit)Nsrange range={4,2};nsstring *substring=[string15 Substringwithrange:range];
//concatenation of stringsnsstring*appstring2=[string16 stringbyappendingformat:@ "%@", String17];nsstring*appstring2=[string16 Stringbyappendingstring:string17];
Find Stringnsstring *[email protected] "ABCDEFGH=_BLANKXYZ"nsrange range=[link rangeofstring;@ "Fgh=_blank"];if (range1.location!=nsnotfound) {//(Nsnotfound=nsintegermax)NSLog (@ "founded");}
variable Stringmsmutablestring *mutablestring=[[nsmutablestring alloc] initwithformat:@ "abc"]Increase[mutableString1 insertstring:@ "XYZ" atindex:3];Change[mutablestring replacecharactersinrang:nsmakerange (0,3) withstring:@ "EFG"];Delete[mutableString1 Deletecharactersinrange:nsmakerange (0,3)];
calculates the size of the text string

-(Cgsize) Sizewithtext: (NSString *) text font: (Uifont *) font maxSize: (cgsize) maxSize

{

Nsdictionary *attrs = @{nsfontattributename:font};

return [text boundingrectwithsize:maxsize options:nsstringdrawinguseslinefragmentorigin attributes:attrs context: Nil].size;

}

OC Traversal string

1) Find the way to (this way suitable for all the format of the substring, recommended to use)

NSString *newstr [email protected] "abdcdddccdd00 everyone good Oh";

NSString *temp = nil;

for (int i =0; i < [newstr length]; i++)

{

temp = [Newstr substringwithrange:nsmakerange (I, 1)];

NSLog (@ "The first%d words are:%@", i,temp);

}

(2) traversing a string by traversing a character (only for strings that do not contain Chinese)

NSString *newstr = @ "abdcdddccdd00";


for (int i =0; i < [newstr length]; i++)

{

NSLog (@ "%d characters are:%@", I, [Newstr characteratindex:i]);

}



Foundation framework--nsstring Class string common operations

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.