NSString----------------of instantiation methodsNSString*str =[[NSString alloc] init]; NSString*str =[[[ NSString alloc] init] autorelease]; Note: There are methods of instantiation and initialization in NSString for example: NSString*STR1 = [NSString stringwithcstring:"New String"enconding:nsaciistringencoding]; NSString*STR2 = [NSString alloc] initwithcstring:"New String"enconding:nsaciistringencoding]; str1 and str2 two objects are the same. The character encoding commonly used in--nsstringencoding----------------nsasciistringencoding nsutf8stringencoding nsunicodestringencoding--nsstring Creating an instance----------------a method with the "@" symbol can only define NSString instances that contain English and numerals, for example: NSString*str ="Hello money~"; --Generate NSString methods containing Chinese-------------//This method automatically frees memory+ (ID) Stringwithcstring: (Const Char*) cString encoding: (nsstringencoding) encoding; //Initialize after Alloc- (ID) Initwithcstring: (Const Char*) cString encoding: (nsstringencoding) encoding; Example: NSString*string= [NSString stringwithcstring:"Hello"encoding:nsutf8stringencoding]; NSString*string= [[NSString alloc] initwithcstring:"Hello"encoding:nsutf8stringencoding]; --use format to create strings-------------+ (ID) stringWithFormat: (NSString *) format ...- (ID) Initwithformat: (NSString *) Format ... Example: NSString*str ="Hello"; NSString*string= [NSString stringWithFormat:@"%@ World", str]; NSLog (string); Result: Hello world--Common substitutions--------------%@ NSString Instance%d,%d,%I integer%u,%U unsigned integer%x displays unsigned integers in hexadecimal lowercase letters%X displays unsigned integers in hexadecimal capitals%F Decimal%C character%s C language string%%displays the% character itself--------------------------Nsrange--definition of nsrange typedefstruct_nsrange {unsignedintLocation ; unsignedintlength;} Nsrange; Nsmakerange Function--this function compares a special return object to a nsrange. Nsmakeranger (unsignedintlocation,unsignedintlength); Example: Nsrange range= Nsmakeranger (0,5); NSLog (@"%d,length is%d", range.location,range.length); ---------------------------Calculating string Lengths-(unsignedint) length; ---------------------------string joins, insertions and deletions1, Connection-(NSString *) stringbyappendingstring: (NSString *)string; -(NSString *) Stringbyappendingformat: (NSString *) format ...; Example: NSString*STR1 =@"Hello"; NSString*STR2 =@" World"; NSString*STR3 =[str1 STRINGBYAPPENDINGSTRING:STR2]; NSString*STR4 = [str2 Stringbyappendingformat:@"%d...%d",Ten, -]; STR4-WorldTen... ------------------generation of nsmutablestring NSString+ (ID)string;//generate an instance of an empty string+ (ID) Stringwithstring: (NSString *)string;//with automatic memory release- (ID) Initwithstring: (NSString *)string; Example: Nsmutablestring*string= [Nsmutablestring stringwithstring:@"Hello"]; 2, append string nsmutablestring+ (void) AppendString: (NSString *)string; - (void) AppendFormat: (NSString *) format ...; Example: Nsmutablestringstring= [nsmutablestringstring]; [stringAppendString:@"Hello"]; [stringAppendString:@" Money"]; [stringAppendString:@" and World"]; 3, insert string nsmutablestring+ (void) Insertstring: (NSString *)stringAtindex: (unsigned) index; Insert a string from the index position for example: nsmutablestring*string= [Nsmutablestring stringwithstring:@"Mac X"]; [stringInsertstring:@"OS"Atindex:4]; string-Mac OS X4, delete string nsmutablestring+ (void) Deletecharactersinrange: (nsrange) range; Example: Nsmutablestring*string= [Nsmutablestring stringwithstring:@"Mac OS"]; [stringDeletecharactersinrange:nsmakeranger (0,1)]; NSLog (string); string-ac os;5, string comparison nsstring-(BOOL) isequaltostring: (NSString *)string; 6, comparing front and rear strings nsstring-(BOOL) Hasprefix: (NSString *)string; -(BOOL) Hassuffix: (NSString *)string; Example: NSString*STR1 =@"Mac OS"; NSString*STR2 =@"Mac Pro"; BOOL Flag; Flag= [str1 hasprefix:@"Mac"]; YES Flag= [str2 Hassuffix:@"OS"]; NO7, String retrieval nsstring//returns the range if found, otherwise Nsrange's location entry is set to Nsnotfound-(Nsrange) rangeofstring: (NSString *) subString; -(Nsrange) rangeofstring: (NSString *) subString option: (unsigned) mask; -(Nsrange) rangeofstring: (NSString *) subString option: (unsigned) Mask range: (nsrange) range; -----mask list of commonly used options Nscaseinsensitivesearch letter case insensitive Nsliteralsearch A comparison of byte units in a string can generally improve the retrieval speed nsbackwardssearch from the end of the range to retrieve Nsanchoredsearch only the front part of the development scope. Ignores the search character in the middle of a string for example: NSString*string=@"Hello World"; Nsrange Range= [stringRangeofstring:@"He"]; if(Range.location! =nsnotfound) {NSLog (@"location=%d,length=%d", range.location,range.length); } 8, intercept string NSString-(NSString *) Substringtoindex: (unsigned) index;//the string that returns the string beginning to the index bit does not contain an index bit-(NSString *) Substringfromindex: (unsigned) index;//string that returns the index bit to the end of the string containing the indexed bit-(NSString *) Substringwithrange: (nsrange) range;//returns a string containing an index bit within a range in a stringExample: NSString*string= [stringSubstringwithrange:nsmakerange (5,2)]; 9, read the text file NSString+ (ID) Stringwithcontentsoffile: (NSString *) path usedencoding: (nsstringencoding *) ENC error: (NSERROR *) error//automatically frees memory- (ID) Initwithcontentsoffile: (NSString *) path encoding: (nsstringencoding) ENC error: (Nserror *) Error Example: NSString*string= [NSString Stringwithcontentsoffile:@"/user/test/yw.txt"Encoding:nsutf8stringencoding error:&ERROR]; if(string){} Ten, output text file NSString-(BOOL) WriteToFile: (NSString *) path atomically: (BOOL) useauxiliaryfile encoding: (nsstringencoding) ENC error: ( Nserror * *) Error//parameter atomically temporarily saves the file to the secondary file//PathThe file to which to write the receiver. If path contains a tilde (~) character, you must expand it withstringbyexpandingtildeinpath before invoking Thismethod. ----Here is an example of an online look thanks @chenshizero//Extension PathNSString *path =@"~/nsdata.txt"; NSString*absolutepath =[Path Stringbyexpandingtildeinpath]; NSLog (@"absolutepath:%@", Absolutepath); NSLog (@"path:%@", [Absolutepath Stringbyabbreviatingwithtildeinpath]); //file name extensionNSString *path =@"~/nsdata.txt"; NSLog (@"extension:%@", [Path pathextension]);
NSString Common methods