扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
字符串对象NSString
成都创新互联公司专注于企业成都全网营销、网站重做改版、扶风网站定制设计、自适应品牌网站建设、H5响应式网站、商城网站开发、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为扶风等各大城市提供网站开发制作服务。
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*)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
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流