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]);