字符串對(duì)象NSString
成都創(chuàng)新互聯(lián)公司專注于企業(yè)成都全網(wǎng)營(yíng)銷、網(wǎng)站重做改版、扶風(fēng)網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5響應(yīng)式網(wǎng)站、商城網(wǎng)站開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為扶風(fēng)等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
Objective-C使用NSString類描述字符串
NSString是一種不可變對(duì)象,即:對(duì)象創(chuàng)建后,值不可改變
NSString提供大量的操作方法:
多樣的創(chuàng)建方式
與數(shù)值的轉(zhuǎn)換
文件/URL操作
子串的獲取
構(gòu)建新串
描述路徑的各種操作
...
字符串對(duì)象NSString及基本操作
創(chuàng)建:init方法和類方法
+ (instancetype)string //空串 + (instancetype)stringWithFormat:(NSString *)format, … //格式化字符串 + (instancetype)stringWithUTF8String:(const char *)bytes //通過(guò)C字符串構(gòu)造
字面值:
NSString *s = @"hello”; //常量方式
基本操作
@property(readonly) const char *UTF8String //c字符串 @property(readonly) NSUInteger length //字符串長(zhǎng)度 - (unichar)characterAtIndex:(NSUInteger)index //獲得字符 - (BOOL)isEqualToString:(NSString *)aString //判斷字符串是否相等
轉(zhuǎn)換成數(shù)值
@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操作相關(guān)
+ (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
獲得子串及構(gòu)建新串
- (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
可變字符串對(duì)象
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