Object-C learning notes NSString common methods
-- Instantiation method --------------
NSString * str = [[NSString alloc] init];
NSString * str = [[NSString alloc] init] autorelease];
Note: NSString contains its own instantiation and initialization methods, for example:
NSString * str1 = [NSString stringWithCString: "new String" enconding: NSACIIStringEncoding];
NSString * str2 = [NSString alloc] initWithCString: "new String" enconding: NSACIIStringEncoding];
The str1 and str2 objects are the same.
-- Common character encoding in NSStringEncoding ----------------
NSASCIIStringEncoding
NSUTF8StringEncoding
NSUnicodeStringEncoding
-- NSString create instance ----------------
A method with the "@" symbol can only define an NSString instance that contains English letters and numbers. For example:
NSString * str = "Hello money ~ ";
-- Generate the NSString method containing Chinese characters -------------
// This method Automatically releases the memory
+ (Id) stringWithCString :( const char *) cString encoding :( NSStringEncoding) encoding;
// Initialize alloc
-(Id) initWithCString :( const char *) cString encoding :( NSStringEncoding) encoding;
For example:
NSString * string = [NSString stringWithCString: "Hello" encoding: NSUTF8StringEncoding];
NSString * string = [[NSString alloc] initWithCString: "Hello" encoding: NSUTF8StringEncoding];
-- Create a string in the format -------------
+ (Id) stringWithFormat :( NSString *) format...
-(Id) initWithFormat :( NSString *) format...
For example:
NSString * str = "hello ";
NSString * string = [NSString stringWithFormat: @ "% @ world", str];
NSLog (string); Result: hello world
-- 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
--------------------------
Nsange
-- 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 );
NSLog (@ "location is % d, length is % d", range. location, range. length );
---------------------------
Calculate the string length
-(Unsigned int) length;
---------------------------
String connection, insertion, and deletion
1. Connection
-(NSString *) stringByAppendingString :( NSString *) string;
-(NSString *) stringByAppendingFormat :( NSString *) format ...;
For example:
NSString * str1 = @ "hello ";
NSString * str2 = @ "world ";
NSString * str3 = [str1 stringByAppendingString: str2];
NSString * str4 = [str2 stringByAppendingFormat: @ "% d... % d", 10, 20];
Str4 --> world 10... 20
-----------------
NSMutableString generation
NSString + (Id) string;// Generate an instance with an empty string
+ (Id) stringWithString :( NSString *) string; // Automatically releases memory
-(Id) initWithString :( NSString *) string;
For example:
NSMutableString * string = [NSMutableString stringWithString: @ "hello"];
2. append a string
NSMutableString
+ (Void) appendString :( NSString *) string;
-(Void) appendFormat :( NSString *) format ...;
For example:
NSMutableString string = [NSMutableString string];
[String appendString: @ "hello"];
[String appendString: @ "money"];
[String appendString: @ "and world"];
3. Insert a string
NSMutableString
+ (Void) insertString :( NSString *) string atIndex :( unsigned) index;
Insert a string from the index position
For example:
NSMutableString * string = [NSMutableString stringWithString: @ "Mac X"];
[String insertString: @ "OS" atIndex: 4];
String --> Mac OS X
4. delete a string
NSMutableString
+ (Void) deleteCharactersInRange :( nsange) range;
For example:
NSMutableString * string = [NSMutableString stringWithString: @ "Mac OS"];
[String deleteCharactersInRange: NSMakeRanger (0, 1)];
NSLog (string );
String --> ac OS;
5. String comparison
NSString
-(BOOL) isEqualToString :( NSString *) string;
6. Compare the front and rear strings
NSString
-(BOOL) hasPrefix :( NSString *) string;
-(BOOL) hasSuffix :( NSString *) string;
For example:
NSString * str1 = @ "Mac OS ";
NSString * str2 = @ "Mac Pro ";
BOOL flag;
Flag = [str1 hasPrefix: @ "Mac"];YES
Flag = [str2 hasSuffix: @ "OS"]; NO
7. string SEARCH
NSString
// If a range is found, the return range is returned. Otherwise, the location item of nsange is set to NSNotFound.
-(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 Case Insensitive
NSLiteralSearch Compares the byte units of a string to increase the retrieval speed.
NSBackwardsSearch Search from the end of the range
NSAnchoredSearch Only the front of the specified range is retrieved. Ignore search characters in the middle of a string
For example:
NSString * string = @ "hello world ";
Nsange range = [string rangeOfString: @ "he"];
If (range. location! = NSNotFound)
{
NSLog (@ "location = % d, length = % d", range. location, range. length );
}
8. truncate a string
NSString
-(NSString *) substringToIndex :( unsigned) index; // Returns a string starting with a string and ending with an index that does not contain an index.
-(NSString *) substringFromIndex :( unsigned) index; // returns the string from the index to the end of the string containing the index bit.
-(NSString *) substringWithRange :( nsange) range; // Returns the index bits contained in the range of the string.
For example:
NSString * string = [string substringWithRange: NSMakeRange (5, 2)];
9. Read text files
NSString
+ (Id) stringWithContentsOfFile:( NSString *) 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];
If (string ){}
10. output text files
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 with stringByExpandingTildeInPath before invoking this method.
---- 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]);