Objective-C使用NSString类描述字符串
NSString是一种不可变对象,即:对象创建后,值不可改变
NSString提供大量的操作方法:
多样的创建方式
与数值的转换
文件/URL操作
子串的获取
构建新串
描述路径的各种操作
...
创建:init方法和类方法
+ (instancetype)string //空串
+ (instancetype)stringWithFormat:(NSString *)format, … //格式化字符串
+ (instancetype)stringWithUTF8String:(const char *)bytes //通过C字符串构造
字面值:
NSString *s = @"hello”; //常量方式
基本操作
@property(readonly) const char *UTF8String //c字符串
@property(readonly) NSUInteger length //字符串长度
- (unichar)characterAtIndex:(NSUInteger)index //获得字符
- (BOOL)isEqualToString:(NSString *)aString //判断字符串是否相等
@property(readonly) double doubleValue
@property(readonly) float floatValue
@property(readonly) int intValue
@property(readonly) NSInteger integerValue
@property(readonly) long long longLongValue
@property(readonly) BOOL boolValue
+ (instancetype)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile
+ (instancetype)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically
- (NSString *)substringFromIndex:(NSUInteger)anIndex
- (NSString *)substringWithRange:(NSRange)aRange
- (NSString *)substringToIndex:(NSUInteger)anIndex
- (NSRange)rangeOfString:(NSString *)aString
- (BOOL)hasPrefix:(NSString *)aString
- (BOOL)hasSuffix:(NSString *)aString
- (NSArray<NSString *> *)componentsSeparatedByString:(NSString *)separator
@property(readonly, copy) NSString *lowercaseString
@property(readonly, copy) NSString *uppercaseString
- (NSString *)stringByAppendingFormat:(NSString *)format, ...
- (NSString *)stringByAppendingString:(NSString *)aString
@property(readonly, copy) NSString *lastPathComponent
@property(readonly, copy) NSString *pathExtension
@property(readonly, copy) NSString *stringByDeletingLastPathComponent
@property(readonly, copy) NSString *stringByDeletingPathExtension
- (NSString *)stringByAppendingPathComponent:(NSString *)aString
- (NSString *)stringByAppendingPathExtension:(NSString *)ext
NSMutableString用于描述可变字符串,是NSString的子类
追加:
- (void)appendString:(NSString *)aString
- (void)appendFormat:(NSString *)format, ...
删除:
- (void) deleteCharactersInRange:(NSRange) aRange
插入/替换:
- (void)insertString:(NSString *)aString atIndex:(NSUInteger)anIndex
- (void)replaceCharactersInRange:(NSRange)aRange withString:(NSString *)aString
- (void)setString:(NSString *)aString