Basic Class-nsstring

Source: Internet
Author: User

Basic knowledge:

Common character encoding in nsstringencoding:

Nsasciistringencoding

Nsutf8stringencoding

Nsunicodestringencoding

 

-- Common replacement character --------------
% @ Nsstring instance
% D, % d, % I integer
% U, % u unsigned integer
% X displays the unsigned integer in hexadecimal lowercase letters
% X: the unsigned integer is displayed in hexadecimal uppercase letters.
% F decimal
% C characters
% S c language string
% Display % characters themselves

 

-- Definition of nsange
Typedef struct _ nsange
{
Unsigned int location;
Unsigned int length;
} Nsange;

Nsmakerange Function
-- This function returns an object of the nsange.
Nsmakeranger (unsigned int location, unsigned int length );

For example:
Nsange range = nsmakeranger (0, 5 );

 

 

1. Method for initializing nsstring:

A. common methods for creating nsstring objects:

Literal mode; alloc + Init; initwithstring method; initwithcstring is created in Standard C mode; stringwithformat is used to create a lattice string;

 

Generation of nsmutablestring: stringwithstring, initwithstring

B. Common Methods:

Calculates the length;

String connection:-(nsstring *) stringbyappendingstring :( nsstring *) string;

-(Nsstring *) stringbyappendingformat :( nsstring *) format ....

 

Append method: + appendstring appendformat

Insert method: + insertstring

Delete method: + deletecharactersinrange

Comparison function: isbench tostring comparison Prefix: hasprefix comparison Suffix: hassuffix

String retrieval (the return type of nsange): three formats of rangeofstring

-(Nsange) rangeofstring :( nsstring *) substring;
-(Nsange) rangeofstring :( nsstring *) substring option :( unsigned) mask;
-(Nsange) rangeofstring :( nsstring *) substring option :( unsigned) mask range: (nsange) range;

------------ List of common mask options -------------
Nscaseinsensitivesearch is case-insensitive
Nsliteralsearch compares the byte units of a string, which generally improves the retrieval speed.
Nsbackwardssearch searches from the end of the range
Nsanchoredsearch only retrieves the front of the specified range. Ignore search characters in the middle of a string

Truncated string: substringtoindex (not including the beginning) substringfromindex substringwithrange

Read text files: + (ID) stringwithcontentsoffile :( nsstirng *) path usedencoding :( nsstringencoding *) ENC error :( nserror **) Error // Automatically releases memory

-(ID) initwithcontentsoffile :( nsstring *) path encoding :( nsstringencoding) ENC error :( nserror **) Error

For example:
Nsstring * string = [nsstring stringwithcontentsoffile: @ "/user/test/yunctxt" encoding: nsutf8stringencoding error: & error];

Output text file:

Nsstring
-(Bool) writetofile :( nsstring *) path atomically :( bool) useauxiliaryfile encoding :( nsstringencoding) ENC error :( nserror **) Error
// The atomically parameter temporarily saves the file to the auxiliary File
// Path
The file to which to write the handler er. If path contains a Tilde (~) Character, you must expand it withstringbyexpandingtildeinpath before invoking this method.

The file must be written to the receiver. If the path contains a symbol (?) Before calling this method, expand withstringbyexpandingtildeinpath.
---- Here is an online search example. Thank you @ chenshizero.
// 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]);

 

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.