基本概念
- (unichar)characterAtIndex:(NSUInteger)index
及@property(readonly) NSUInteger length
。
對於字符串的比較、查找等等都是以字符為基礎的。也就是說比較時,是挨個字符(character)比較的。
unichar
是UTF-16編碼單位。typealias unichar = UInt16
length
方法與charAtIndex
方法。
另一個選擇是聲明一個類,將NSString作為一個屬性,並將需要的其它的metadata也作為屬性存儲在類中。並實現需要用到的NSString的方法(其實就是調用這個類NSString屬性的方法)及其他方法。
category。 對所有的NSString對象都會起作用,有時會有導致意想不到的效果。不過可以增加NSString的功能。
- (instancetype)initWithBytes:(const void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding;
unichar
數組中生成。- (instancetype)initWithCharacters:(const unichar *)characters length:(NSUInteger)length;
從C字符串中的生成- (instancetype)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding;
- (instancetype)initWithFormat:(NSString *)format locale:(id)locale arguments:(va_list)argList;
從NSData創建- (instancetype)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding;
data
中,按照encoding
解析,得到UTF-16的code unit序列。
從文件中創建- (instancetype)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
- (instancetype)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError * _Nullable *)error;
- (instancetype)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
返回解析用的編碼格式- (instancetype)initWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError * _Nullable *)error;
寫到文件或URL中
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
useAuxiliaryFile
是YES
,那麼會生成一個臨時的文件,把String寫到這個文件中,然後將這個文件重命名為目標文件。這保證了及時系統crash,也不會導致已存在的文件(如果存在)會損壞。com.apple.TextEncodin
,值是編碼的IANA名字+分號+編碼格式的CFStringEncoding值。從文件裡生成字符串時,也是用這些個信息來決定編碼格式的。例如dd = @"d";
NSString *file = [NSHomeDirectory() stringByAppendingPathComponent:@"test"];
NSError *error = nil;
[dd writeToFile:file atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (nil == error)
{
NSLog(@"write success");
}
NSDictionary *attrbuteDict = [[NSFileManager defaultManager] attributesOfItemAtPath:file error:&error];
NSDictionary *extendAtt = [attrbuteDict objectForKey:@"NSFileExtendedAttributes"];
NSData *attrData = [extendAtt objectForKey:@"com.apple.TextEncoding"];
NSString *attrStr = [[NSString alloc] initWithData:attrData encoding:NSUTF8StringEncoding];

- 寫到URL中去
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
獲取長度
@property(readonly) NSUInteger length
。不是打印出來字符的長度。
使用某種編碼格式編碼,比特數組的長度。- (NSUInteger)lengthOfBytesUsingEncoding:(NSStringEncoding)enc;
時間復雜度O(n)

使用某種格式編碼,比特數組的不精確上界。- (NSUInteger)maximumLengthOfBytesUsingEncoding:(NSStringEncoding)enc;
。時間復雜度O(1).獲取UTF-16編碼單位的數組
- (void)getCharacters:(unichar *)buffer range:(NSRange)range;
buffer
必須足夠大。
- (BOOL)getBytes:(void *)buffer maxLength:(NSUInteger)maxBufferCount usedLength:(NSUInteger *)usedBufferCount encoding:(NSStringEncoding)encoding options:(NSStringEncodingConversionOptions)options range:(NSRange)range remainingRange:(NSRangePointer)leftover;
獲取C字符串(以NULL結尾的字符串)
調用之前先使用- (BOOL)canBeConvertedToEncoding:(NSStringEncoding)encoding;
方法確定是否可以無失真的編碼。
這些方法都不要調用UTF-16,UTF-32編碼格式,因為這些包括NULL字節。
- (const char *)cStringUsingEncoding:(NSStringEncoding)encoding;
- (BOOL)getCString:(char *)buffer maxLength:(NSUInteger)maxBufferCount encoding:(NSStringEncoding)encoding;
buffer
需要自己分配內存,因此生命周期自己控制。
字符串的比較
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToCompare locale:(id)locale;
- (BOOL)hasSuffix:(NSString *)str;
- (BOOL)hasSuffix:(NSString *)str;
- (BOOL)isEqualToString:(NSString *)aString;
isEqual:
方法要快一些。
組合字符串
stringByAppendingFormat:
- (NSString *)stringByPaddingToLength:(NSUInteger)newLength withString:(NSString *)padString startingAtIndex:(NSUInteger)padIndex;
padString
的padIndex
開始填充。
分隔字符串
- (NSArray<NSString *> *)componentsSeparatedByString:(NSString *)separator;
- (NSArray<NSString *> *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator;
- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set;
- (NSString *)substring***
類的方法
查找字符串
- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)searchSet options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToSearch;
字符串查找- (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToSearch locale:(NSLocale *)locale;
按行枚舉- (void)enumerateLinesUsingBlock:(void (^)(NSString *line, BOOL *stop))block;
按照NSStringEnumerationOptions
枚舉- (void)enumerateSubstringsInRange:(NSRange)range options:(NSStringEnumerationOptions)opts usingBlock:(void (^)(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop))block;
NSStringEnumerationOptions
字符串替換
分行和分頁
ComposedCharacter
我理解就是由多個UTF16 code unit表示的字符。為了避免被截斷,需要確定包含指定index的完整的ComposedCharacter。
- (NSRange)rangeOfComposedCharacterSequenceAtIndex:(NSUInteger)index;
確定String在屏幕上的大小
- (CGSize)sizeWithAttributes:(NSDictionary<NSString *,id> *)attrs;
這個函數返回的包括小數,因此需要對返回值調用ceil
函數。
stringByFoldingWithOptions:locale
????
找到共同的前綴
- (NSString *)commonPrefixWithString:(NSString *)str options:(NSStringCompareOptions)mask;
編碼相關
- (BOOL)canBeConvertedToEncoding:(NSStringEncoding)encoding;
根據NSData來探測這個NSData的編碼格式+ (NSStringEncoding)stringEncodingForData:(NSData *)data encodingOptions:(NSDictionary<NSStringEncodingDetectionOptionsKey,id> *)opts convertedString:(NSString * _Nullable *)string usedLossyConversion:(BOOL *)usedLossyConversion;
String Encoding Detection Options
U+FFFD
。
likelyLanguageKey- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding allowLossyConversion:(BOOL)lossy;
無失真編碼下的最快編碼格式@property(readonly) NSStringEncoding fastestEncoding;
無失真編碼格式下占用空間最小的格式@property(readonly) NSStringEncoding smallestEncoding
URL相關
- (NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters;
@property(readonly, copy) NSString *stringByRemovingPercentEncoding;
文件路徑相關
class func path(withComponents components: [String]) -> String
把路徑用分隔符分隔,得到數組var pathComponents: [String] { get }
把路徑表示成系統形式的c字符串
@property(readonly) const char *fileSystemRepresentation;
char filenameBuffer[13];
BOOL success;
success = [@"/mach_kernel" getFileSystemRepresentation:filenameBuffer maxLength:12];
// success == NO
// Changing the length to include the NULL character does work
success = [@"/mach_kernel" getFileSystemRepresentation:filenameBuffer maxLength:13];
// success == YES
獲取最後一個路徑名字
@property(readonly, copy) NSString *lastPathComponent;

路徑擴展名
@property(readonly, copy) NSString *pathExtension;
處理~
~
擴展為絕對路徑@property(readonly, copy) NSString*stringByExpandingTildeInPath;
絕對路徑縮寫~
@property(readonly, copy) NSString *stringByAbbreviatingWithTildeInPath;
在路徑名後加子路徑
- (NSString *)stringByAppendingPathComponent:(NSString *)str;
路徑後加後綴
- (NSString *)stringByAppendingPathExtension:(NSString *)str;
刪除最後一段路徑
@property(readonly, copy) NSString *stringByDeletingLastPathComponent;
刪除路徑後綴
@property(readonly, copy) NSString *stringByDeletingPathExtension;
解析符號鏈接(快捷方式)
@property(readonly, copy) NSString *stringByResolvingSymlinksInPath;
標准化路徑
@property(readonly, copy) NSString *stringByStandardizingPath;
~
號為絕對路徑
如果可以,移除起始的“/private/var/automount”
, “/var/automount”
或 “/private”
。
處理//
或/./
移除最後一個路徑段的尾斜線
處理符號鏈接如../
更多知識