Some common methods of nsstring in objective-C Foundation

Source: Internet
Author: User

[STR hasprefix: @ "12"]; // return value bool starting with 12
[STR hassuffix: @ "TXT"]; // whether to return bool at the end of txt

// Search for strings
[STR rangeofstring: @ "345"]; // returns the return location of the nsange parameter. If the return location is not found, {nsnotfound, 0} is returned}
// Search from the end
Nsange range = [STR rangeofstring: @ "456" Options: nsbackkwardssearch];

# Pragma mark string Truncation
Nsstring * STR = @ "123456 ";
// Truncate from index 3 to the end
Nslog (@ "% @", [STR substringfromindex: 3]); // print 456
// Truncate from the header to before index 3 (excluding 3)
Nslog (@ "% @", [STR substringtoindex: 3]); // print 123
// Intercept within the specified range
Nsange range = nsmakerange (2, 3 );
Nslog (@ "% @", [STR substringwithrange: range]); // print 345

Nsstring * str2 = @ "1, 2, 3, 4, 5 ";
// Separated by commas
Nsarray * array = [str2 componentsseparatebystirng: @ ","];
Nslog (@ "% @", array );

Nsstring * str3 = [array objectatindex: 0];
Nslog (@ "% @", str3); // print 1

Nsstring and Path:

# Pragma mark
Nsarray * components = [nsmutablearray array]; // nsmutablearray variable
[Components addobject: @ "users"];
[Components addobject: @ "gongpb"];
[Components addobject: @ "desktop"];
// Concatenates all strings in the array into a path
Nsstring * Path = [nsstring pathwithcompoents: components]; // print users/gongpb/desktop
Nslog (@ "% @", PATH );

Nsarray * CMPs = [path pathcomponents]; // Decomposition
Nslog (@ "% @", CMPs); // (users, gongpb, desktop)

Path = @ "users/gongpb/desktop/test ";
Nslog (@ "% I", [path isabsolutepath]); // 0 is returned to determine whether the front edge has a slash/
// The last folder
Nslog (@ "Last Directory: % @", [path lastpathcomponent]); // return test

// Delete the last folder
Nslog (@ "% @", [path stringbydeletinglastpathcomponent]); returns users/gongpb/desktop deleted/test

// Concatenate a spoof directory at the end
Nslog (@ "% @", [path stringbyappendingpathcomponent: @ "ABC"]); // return/users/gongpb/desktop/test/ABC

Nsstring * STR = @ "/user/gongpb/test.txt ";
Nslog (@ "% @", [STR pathextension]); // get the extension,
// Delete the extension
[STR stringbydeletingpathextension];
// Add Extension
[@ "ABC" stringbyappendingpathextension: @ "MP3"];

Other usage:
Nsstring * STR = @ "12 ";
Int A = [STR intvalue]; // convert int

// Calculate the number of words, not the number of characters
[STR length];

// Obtain characters
Unichar c = [@ "ABC" characteratindex: 0];
Nslog (@ "% C", C );

// Returns the const string in the C language, indicating that S is a constant.
Const char * s = [@ "ABC", utf8string];
Nslog (@ "% s", S );

Related Article

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.