Common Objective-C NSString and nsstring Methods
Tip: the previous ones are more common.
1. Create a string
Instance method:
-(Instancetype) initWithFormat :( NSString *) format
Format to generate a String object
This method is commonly used and has powerful functions. Usage: [[NSString alloc] initWithFormat: @ "I am student at % @ of Technology", @ "GuangDong University"];
-(Instancetype) initWithString :( NSString *) aString
Generates a String object based on the given string.
-(Instancetype) initwithuf8string :( const char *) bytes
UTF-8 encoding format C string to generate a String object
-(Instancetype) initWithCharacters :( const unichar *) characters
Length :( NSUInteger) length
Generates a String object based on the given C string (unencoded) and string length.
Class method:
+ (Instancetype) stringWithFormat :( NSString *) format ,...
Format and generate a string
+ (Instancetype) stringWithString :( NSString *) aString
+ (Instancetype) stringWithCharacters :( const unichar *) chars
Length :( NSUInteger) length
+ (Instancetype) stringWithCString :( const char *) cString
Encoding :( NSStringEncoding) enc
+ (Instancetype) stringwithuf8string :( const char *) bytes
2. Common attributes
@ Property (readonly) NSUInteger length
Returns the string length.
@ Property (readonly, copy) NSString * uppercaseString;
Converts lowercase letters in a string to uppercase letters.
@ Property (readonly, copy) NSString * lowercaseString;
Converts uppercase letters in a string to lowercase letters.
@ Property (readonly, copy) NSString * capitalizedString;
Converts the first letter of a word in a string to a uppercase letter, and the rest to lowercase letters.
@ Property (readonly) double doubleValue
@ Property (readonly) float floatValue
@ Property (readonly) int intValue
@ Property (readonly) NSInteger integerValue
@ Property (readonly) long longLongValue
@ Property (readonly) BOOL boolValue
Obtain the basic data type of the string.
Call method: str. uppercaseString; str. lowercaseString; str. capitalizedString (Instance name. attribute name)
3. Common Methods:
-(NSString *) stringByAppendingFormat :( NSString *) format ,...
Add a formatted string after the string
-(NSString *) substringFromIndex :( NSUInteger)AnIndex
Substring truncation (starting from anIndex position character (inclusive) to string end)
-(NSString *) substringToIndex :( NSUInteger)AnIndex
Substring truncation (truncates a string of anIndex characters from 0)
-(BOOL) containsString :( NSString *)Str
Judge whether the string contains the str string
-(BOOL) hasPrefix :( NSString *)AString
Whether the string contains the aString prefix
-(BOOL) hasSuffix :( NSString *)AString
Whether the string contains the aString suffix
-(BOOL) isw.tostring :( NSString *)AString
Judge whether the string and aString are equal
-(Nsange) rangeOfString :( NSString *)AString
Search for the range of the aString in the string that calls the method (start position and length)
-(NSString *) stringByReplacingOccurrencesOfString :( NSString *)TargetWithString :( NSString *)Replacement
Replacement string with the substring target in the string
-(NSString *) stringByReplacingCharactersInRange :( nsange)Range
WithString :( NSString *)Replacement
Replace the string in the range specified by range in the string with the replacement string.
-(NSComparisonResult) compare :( NSString *)AString
Compare string and astring
-(NSString *) stringByAppendingString :( NSString *) aString
Add a string aString after the string
-(NSString *) substringWithRange :( nsange)ARange
Substring truncation (the range is determined by the location and length in aRange)
Use NSMakeRange (NSUInteger loc, NSUInteger len) to obtain
-(Unichar) characterAtIndex :( NSUInteger) index
Obtains the character at the index position of the string (starting from 0 ).
-(Void) getCharacters :( unichar *) buffer
Store string content characters in the cache buffer
-(Void) getCharacters :( unichar *) buffer
Range :( nsange) aRange
Store the characters in the range specified by range to the buffer.
Note:
NSMutableString (variable string)
Variable classes must be initialized before definition.
-(Void) appendFormat :( NSString *)Format,...
Append the formatted string after the string
-(Void) appendString :( NSString *)AString
Add a string aString after the string
-(Void) deleteCharactersInRange :( nsange)ARange
Deletes a string in the range specified by aRange.
-(Void) insertString :( NSString *)AString
AtIndex :( NSUInteger)AnIndex
Insert the string aString at the anIndex position
-(Void) replaceCharactersInRange :( nsange)ARange
WithString :( NSString *)
AString
Replace the string in the range specified by aRange with aString.
-(Void) setString :( NSString *)AString
Set the string to the specified string aString