Foundation框架字符串对象
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Foundation框架字符串对象相关的知识,希望对你有一定的参考价值。
字符串对象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
本文出自 “teacherAn” 博客,请务必保留此出处http://annmeng.blog.51cto.com/3321237/1745708
以上是关于Foundation框架字符串对象的主要内容,如果未能解决你的问题,请参考以下文章