字符串对象NSString
Objective-C使用NSString类描述字符串
NSString是一种不可变对象,即:对象创建后,值不可改变
NSString提供大量的操作方法:
多样的创建方式
与数值的转换
文件/URL操作
子串的获取
构建新串
描述路径的各种操作
...
字符串对象NSString及基本操作
创建: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
文件及URL操作相关
+ (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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。