标签:
- (unichar)characterAtIndex:(NSUInteger)index
及@property(readonly) NSUInteger length
。unichar
是UTF-16编码单位。typealias unichar = UInt16
length
方法与charAtIndex
方法。- (instancetype)initWithBytes:(const void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding;
unichar
数组中生成。- (instancetype)initWithCharacters:(const unichar *)characters length:(NSUInteger)length;
- (instancetype)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding;
- (instancetype)initWithFormat:(NSString *)format locale:(id)locale arguments:(va_list)argList;
- (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;
+ (NSStringEncoding)stringEncodingForData:(NSData *)data encodingOptions:(NSDictionary<NSStringEncodingDetectionOptionsKey,id> *)opts convertedString:(NSString * _Nullable *)string usedLossyConversion:(BOOL *)usedLossyConversion;
String Encoding Detection Options
U+FFFD
。- (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”
。//
或/./
../
更多知识
标签:
原文地址:http://www.cnblogs.com/huahuahu/p/NSString-xue-xi.html