NSString和NSMutableString整理与总结

一、NSString

1.NSString初始化方法

NSString *string = @"string";
- (instancetype)initWithString:(NSString *)aString;
- (instancetype)initWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
+ (instancetype)stringWithString:(NSString *)string;
+ (instancetype)stringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);

2.NSString的长度获取

@property (readonly) NSUInteger length;

3.NSString获取字符和字符串

(1)获取字符串中index对应位置的单个字符

- (unichar)characterAtIndex:(NSUInteger)index;

(2)获取字符串

①获取从字符串首位到from位置的字符串

- (NSString *)substringFromIndex:(NSUInteger)from;

②获取从字符串to位置到末位的字符串

- (NSString *)substringToIndex:(NSUInteger)to;

③获取字符串中range范围内的字符串

- (NSString *)substringWithRange:(NSRange)range; 
// Use with rangeOfComposedCharacterSequencesForRange: to avoid breaking up character sequences

④我们可以使用以下方法来防止获取字符串时一些特殊字符串被

/* To avoid breaking up character sequences such as Emoji, you can do:*/
[str substringFromIndex:[str rangeOfComposedCharacterSequenceAtIndex:index].location];
[str substringToIndex:NSMaxRange([str rangeOfComposedCharacterSequenceAtIndex:index])];
[str substringWithRange:[str rangeOfComposedCharacterSequencesForRange:range];

⑤字符串拼接

- (NSString *)stringByAppendingString:(NSString *)aString;
- (NSString *)stringByAppendingFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);

⑥字符串替换

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange NS_AVAILABLE(10_5, 2_0);

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement NS_AVAILABLE(10_5, 2_0);

- (NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement NS_AVAILABLE(10_5, 2_0);

4.NSString字符串查找与判断

(1)判断字符串是否完全相等

- (BOOL)isEqualToString:(NSString *)aString;

(2)判断字符串中是否含有str

- (BOOL)containsString:(NSString *)str NS_AVAILABLE(10_10, 8_0);
- (BOOL)localizedCaseInsensitiveContainsString:(NSString *)str NS_AVAILABLE(10_10, 8_0);//忽略大小写

(3)判断字符串中是否含有前缀str

- (BOOL)hasPrefix:(NSString *)str;

(4)判断字符串中是否含有后缀str

- (BOOL)hasSuffix:(NSString *)str;

(5)比较字符串

- (NSComparisonResult)compare:(NSString *)string;
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;

/** 忽略大小写进行比较 */
- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;

(6)查找字符串,未找到返回length==0

/* These methods perform string search, looking for the searchString within the receiver string.  These return length==0 if the target string is not found. So, to check for containment: ([str rangeOfString:@"target"].length > 0).  Note that the length of the range returned by these methods might be different than the length of the target string, due composed characters and such.*/

- (NSRange)rangeOfString:(NSString *)searchString;
- (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask;
- (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask range:(NSRange)searchRange;

(7)查找字符

/* These return the range of the first character from the set in the string, not the range of a sequence of characters. */
- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)searchSet;
- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)searchSet options:(NSStringCompareOptions)mask;
- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)searchSet options:(NSStringCompareOptions)mask range:(NSRange)searchRange;

- (NSRange)rangeOfComposedCharacterSequenceAtIndex:(NSUInteger)index;
- (NSRange)rangeOfComposedCharacterSequencesForRange:(NSRange)range NS_AVAILABLE(10_5, 2_0);

5.NSString转换

(1)字符串大小写转换

@property (readonly, copy) NSString *uppercaseString;   //大写
@property (readonly, copy) NSString *lowercaseString;   //小写
@property (readonly, copy) NSString *capitalizedString; //首字母大写

(2)NSString转换为C风格字符串

①C风格字符串转换成NSString

- (nullable instancetype)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding;
+ (nullable instancetype)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc;

②NSString转换成C风格字符串

- (nullable __strong const char *)cStringUsingEncoding:(NSStringEncoding)encoding NS_RETURNS_INNER_POINTER; 
- (BOOL)getCString:(char *)buffer maxLength:(NSUInteger)maxBufferCount encoding:(NSStringEncoding)encoding; 

(3)NSString转换为基本类型

@property (readonly) double doubleValue;
@property (readonly) float floatValue;
@property (readonly) int intValue;
@property (readonly) NSInteger integerValue NS_AVAILABLE(10_5, 2_0);
@property (readonly) long long longLongValue NS_AVAILABLE(10_5, 2_0);
@property (readonly) BOOL boolValue NS_AVAILABLE(10_5, 2_0); 

(4)NSString和CGGeometry互相转换

①CGGeometry转换成NSString

UIKIT_EXTERN NSString *NSStringFromCGPoint(CGPoint point);
UIKIT_EXTERN NSString *NSStringFromCGSize(CGSize size);
UIKIT_EXTERN NSString *NSStringFromCGRect(CGRect rect);
UIKIT_EXTERN NSString *NSStringFromCGAffineTransform(CGAffineTransform transform);
UIKIT_EXTERN NSString *NSStringFromUIEdgeInsets(UIEdgeInsets insets);
UIKIT_EXTERN NSString *NSStringFromUIOffset(UIOffset offset);

②NSString转换成CGGeometry

UIKIT_EXTERN CGPoint CGPointFromString(NSString *string);
UIKIT_EXTERN CGSize CGSizeFromString(NSString *string);
UIKIT_EXTERN CGRect CGRectFromString(NSString *string);
UIKIT_EXTERN CGAffineTransform CGAffineTransformFromString(NSString *string);
UIKIT_EXTERN UIEdgeInsets UIEdgeInsetsFromString(NSString *string);
UIKIT_EXTERN UIOffset UIOffsetFromString(NSString *string);

(5)NSString和NSRange互相转换

FOUNDATION_EXPORT NSString *NSStringFromRange(NSRange range);
FOUNDATION_EXPORT NSRange NSRangeFromString(NSString *aString);

(6)NSString和Class互相转换

FOUNDATION_EXPORT NSString *NSStringFromClass(Class aClass);
FOUNDATION_EXPORT Class __nullable NSClassFromString(NSString *aClassName);

(7)NSString和Protocol互相转换

FOUNDATION_EXPORT NSString *NSStringFromProtocol(Protocol *proto) NS_AVAILABLE(10_5, 2_0);
FOUNDATION_EXPORT Protocol * __nullable NSProtocolFromString(NSString *namestr) NS_AVAILABLE(10_5, 2_0);

(8)NSString和SEL互相转换

FOUNDATION_EXPORT NSString *NSStringFromSelector(SEL aSelector);
FOUNDATION_EXPORT SEL NSSelectorFromString(NSString *aSelectorName);

(9)NSString和NSArray互相转换

①NSArray转换成NSString

- (NSString *)componentsJoinedByString:(NSString *)separator;

②NSString转换成NSArray(分割字符串)

- (NSArray<NSString *> *)componentsSeparatedByString:(NSString *)separator;
- (NSArray<NSString *> *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator NS_AVAILABLE(10_5, 2_0);

(10)NSString和NSData互相转换

①NSData转换成NSString

- (nullable instancetype)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding;

②NSString转换成NSData

- (nullable NSData *)dataUsingEncoding:(NSStringEncoding)encoding;

6.保存字符串

/* Write to specified url or path using the specified encoding.  The optional error return is to indicate file system or encoding errors. */
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;

二、NSMutableString

NSMutableString继承自NSString,所有的NSString的方法NSMutableString都可以调用.

1.NSMutableString初始化

- (NSMutableString *)initWithCapacity:(NSUInteger)capacity;
+ (NSMutableString *)stringWithCapacity:(NSUInteger)capacity;

2.NSMutableString操作

(1)插入字符串

- (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc;

(2)删除字符串

- (void)deleteCharactersInRange:(NSRange)range;

(3)拼接字符串

- (void)appendString:(NSString *)aString;
- (void)appendFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);

(4)重置字符串

- (void)setString:(NSString *)aString;

(5)替换字符串

- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString;
- (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange;

你可能感兴趣的:(NSString)