前言
NSString虽然我们每天都用也很简单,这里我就总结一下,做到NSString简单细说深说,这里首先要对整个NSString有整体的感知和认识,详细的以后会续写,不对的地方大家批评补充。
苹果开发文档
NSString功能架构
下面我们开始,首先看一下NSString有哪些功能相对应的属性和类。
功能 | 方法名 |
---|---|
简单实例化 | 1. + (instancetype) string; |
2. - (instancetype) init | |
3. - (instancetype) initWithBytes:(const void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding; | |
4. - (instancetype) initWithBytesNoCopy:(void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding freeWhenDone:(BOOL)freeBuffer; | |
5. - (instancetype) initWithCharacters:(const unichar *)characters length:(NSUInteger)length; | |
6. - (instancetype) initWithCharactersNoCopy:(unichar *)characters length:(NSUInteger)length freeWhenDone:(BOOL)freeBuffer; | |
7. - (instancetype) initWithString:(NSString *)aString; | |
8. - (instancetype) initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding; | |
9. - (instancetype) initWithUTF8String:(const char *)nullTerminatedCString; | |
10. - (instancetype) initWithFormat:(NSString *)format, ...; | |
11. - (instancetype) initWithFormat:(NSString *)format arguments:(va_list)argList; | |
12. - (instancetype) initWithFormat:(NSString *)format locale:(id)locale, ...; | |
13. - (instancetype) initWithFormat:(NSString *)format locale:(id)locale arguments:(va_list)argList; | |
14. - (instancetype) initWithData:(NSData *)data encoding:(NSStringEncoding)encoding; | |
15. + (instancetype) stringWithFormat:(NSString *)format, ...; | |
16. + (instancetype) localizedStringWithFormat:(NSString *)format, ...; | |
17. + (NSString ) localizedUserNotificationStringForKey:(NSString)key arguments:(NSArray*)arguments; | |
18. + (instancetype) stringWithCharacters:(const unichar *)characters length:(NSUInteger)length; | |
19. + (instancetype) stringWithString:(NSString *)string; | |
20. + (instancetype) stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc; | |
21. + (instancetype) stringWithUTF8String:(const char *)nullTerminatedCString; | |
文件实例化 | 1. + (instancetype) stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error; |
2. - (instancetype) initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error; | |
3. + (instancetype) stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError * _Nullable *)error; | |
4. - (instancetype) initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError * _Nullable *)error; | |
URL实例化 | 1. + (instancetype) stringWithContentsOfURL:(NSURL*)url encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error; |
2. - (instancetype) initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error; | |
3. + (instancetype) stringWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError * _Nullable *)error; | |
4. - (instancetype) initWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError * _Nullable *)error; | |
写向文件和URL | 1. - (BOOL) writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error; |
2. - (BOOL) writeToURL:(NSURL *)url atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error; | |
获取字符串长度 | 1. @property(readonly) NSUInteger length; |
2. - (NSUInteger) lengthOfBytesUsingEncoding:(NSStringEncoding)enc; | |
3. - (NSUInteger) maximumLengthOfBytesUsingEncoding:(NSStringEncoding)enc; | |
获取字符字节 | 1. - (unichar) characterAtIndex:(NSUInteger)index; |
2. - (void) getCharacters:(unichar *)buffer range:(NSRange)range; | |
3. - (BOOL) getBytes:(void *)buffer maxLength:(NSUInteger)maxBufferCount usedLength:(NSUInteger *)usedBufferCount encoding:(NSStringEncoding)encoding options:(NSStringEncodingConversionOptions)options range:(NSRange)range remainingRange:(NSRangePointer)leftover; | |
获取C字符串 | 1. - (const char *) cStringUsingEncoding:(NSStringEncoding)encoding; |
2. - (BOOL) getCString:(char *)buffer maxLength:(NSUInteger)maxBufferCount encoding:(NSStringEncoding)encoding; | |
3. @property(readonly) const char *UTF8String; | |
字符串比较 | 1. - (NSComparisonResult)caseInsensitiveCompare:(NSString *)string; |
2. - (NSComparisonResult) localizedCaseInsensitiveCompare:(NSString *)string; | |
3. - (NSComparisonResult) compare:(NSString *)string; | |
4. - (NSComparisonResult) localizedCompare:(NSString *)string; | |
5. - (NSComparisonResult) compare:(NSString *)string options:(NSStringCompareOptions)mask; | |
6. - (NSComparisonResult) compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToCompare; | |
7. - (NSComparisonResult) compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToCompare locale:(id)locale; | |
8. - (NSComparisonResult) localizedStandardCompare:(NSString *)string; | |
9. - (BOOL) hasPrefix:(NSString *)str; | |
10. - (BOOL) hasSuffix:(NSString *)str; | |
11. - (BOOL) isEqualToString:(NSString *)aString; | |
12. @property(readonly) NSUInteger hash; | |
字符串的合并 | 1. - (NSString *) stringByAppendingFormat:(NSString *)format, ...; |
2. - (NSString *)stringByAppendingString:(NSString *)aString; | |
3. - (NSString *) stringByPaddingToLength:(NSUInteger)newLength withString:(NSString *)padString startingAtIndex:(NSUInteger)padIndex; | |
字符串的分割 | 1. - (NSArray |
2. - (NSArray |
|
3. - (NSString *) stringByTrimmingCharactersInSet:(NSCharacterSet *)set; | |
4. - (NSString *) substringFromIndex:(NSUInteger)from; | |
5. - (NSString *) substringWithRange:(NSRange)range; | |
6. - (NSString *) substringToIndex:(NSUInteger)to; | |
字符串的查询 | 1. - (BOOL) containsString:(NSString *)str; |
2. - (BOOL) localizedCaseInsensitiveContainsString:(NSString *)str; | |
3. - (BOOL) localizedStandardContainsString:(NSString *)str; | |
4. - (NSRange) rangeOfCharacterFromSet:(NSCharacterSet *)searchSet; | |
5. - (NSRange) rangeOfCharacterFromSet:(NSCharacterSet *) searchSet options:(NSStringCompareOptions)mask; | |
6. - (NSRange) rangeOfCharacterFromSet:(NSCharacterSet *)searchSet options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToSearch; | |
7. - (NSRange) rangeOfString:(NSString *)searchString; | |
8. - (NSRange) rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask; | |
9. - (NSRange) rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToSearch; | |
10. - (NSRange) rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToSearch locale:(NSLocale *)locale; | |
11. - (NSRange) localizedStandardRangeOfString:(NSString *)str; | |
12. - (void) enumerateLinesUsingBlock:(void (^)(NSString *line, BOOL *stop))block; | |
13. - (void) enumerateSubstringsInRange:(NSRange)range options:(NSStringEnumerationOptions)opts usingBlock:(void (^)(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop))block; | |
子字符串替换 | 1. - (NSString *) stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement; |
2. - (NSString *) stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange; | |
3. - (NSString *) stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement; | |
线和段落区间 | 1. - (void) getLineStart:(NSUInteger *)startPtr end:(NSUInteger *)lineEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range; |
2. - (NSRange) lineRangeForRange:(NSRange)range; | |
3. - (void) getParagraphStart:(NSUInteger *)startPtr end:(NSUInteger *)parEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range; | |
4. - (NSRange) paragraphRangeForRange:(NSRange)range; | |
返回给定区间 | 1. - (NSRange) rangeOfComposedCharacterSequenceAtIndex:(NSUInteger)index; |
2. - (NSRange) rangeOfComposedCharacterSequencesForRange:(NSRange)range; | |
转化属性列表 | 1. - (id) propertyList; |
2. - (NSDictionary *) propertyListFromStringsFileFormat; | |
画字符串 | 1. - (void) drawAtPoint:(CGPoint)point withAttributes:(NSDictionary |
2. - (void) drawInRect:(CGRect)rect withAttributes:(NSDictionary |
|
3. - (void) drawWithRect:(CGRect)rect options:(NSStringDrawingOptions)options attributes:(NSDictionary |
|
4. - (CGRect) boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary |
|
5. - (CGSize) sizeWithAttributes:(NSDictionary |
|
折叠 | 1. - (NSString *) stringByFoldingWithOptions:(NSStringCompareOptions)options locale:(NSLocale *)locale; |
字符串前缀分享 | 1. - (NSString *) commonPrefixWithString:(NSString *)str options:(NSStringCompareOptions)mask; |
形改变 | 1. @property(readonly, copy) NSString *lowercaseString; |
2. @property(readonly, copy) NSString *localizedLowercaseString; | |
3. - (NSString *)lowercaseStringWithLocale:(NSLocale *)locale; | |
4. @property(readonly, copy) NSString *uppercaseString; | |
5. @property(readonly, copy) NSString *localizedUppercaseString; | |
6. - (NSString *) uppercaseStringWithLocale:(NSLocale *)locale; | |
7. @property(readonly, copy) NSString *capitalizedString; | |
8. @property(readonly, copy) NSString *localizedCapitalizedString; | |
9. - (NSString *) capitalizedStringWithLocale:(NSLocale *)locale; | |
映射 | 1. @property(readonly, copy) NSString *decomposedStringWithCanonicalMapping; |
2. @property(readonly, copy) NSString *decomposedStringWithCompatibilityMapping; | |
3. @property(readonly, copy) NSString *precomposedStringWithCanonicalMapping; | |
4. @property(readonly, copy) NSString *precomposedStringWithCompatibilityMapping; | |
获取数值 | 1. @property(readonly) double doubleValue; |
2. @property(readonly) float floatValue; | |
3. @property(readonly) int intValue; | |
4. @property(readonly) NSInteger integerValue; | |
5. @property(readonly) long long longLongValue; | |
6. @property(readonly) BOOL boolValue; | |
编码 | 1. + (NSStringEncoding) stringEncodingForData:(NSData *)data encodingOptions:(NSDictionary |
2. + (NSString *) localizedNameOfStringEncoding:(NSStringEncoding)encoding; | |
3. - (BOOL) canBeConvertedToEncoding:(NSStringEncoding)encoding; | |
4. - (NSData *) dataUsingEncoding:(NSStringEncoding)encoding; | |
5. - (NSData *) dataUsingEncoding:(NSStringEncoding)encoding allowLossyConversion:(BOOL)lossy; | |
6. @property(readonly, copy) NSString *description; | |
7. @property(readonly) NSStringEncoding fastestEncoding; | |
8. @property(readonly) NSStringEncoding smallestEncoding; | |
路经相关 | 1. + (NSString *) pathWithComponents:(NSArray |
2. @property(readonly, copy) NSArray |
|
3. - (NSUInteger) completePathIntoString:(NSString * _Nullable *)outputName caseSensitive:(BOOL)flag matchesIntoArray:(NSArray |
|
4. @property(readonly) const char *fileSystemRepresentation; | |
5. - (BOOL) getFileSystemRepresentation:(char *)cname maxLength:(NSUInteger)max; | |
6. @property(getter=isAbsolutePath, readonly) BOOL absolutePath; | |
7. @property(readonly, copy) NSString *lastPathComponent; | |
8. @property(readonly, copy) NSString *pathExtension; | |
9. @property(readonly, copy) NSString *stringByAbbreviatingWithTildeInPath; | |
10. - (NSString *) stringByAppendingPathComponent:(NSString *)str; | |
11.- (NSString *) stringByAppendingPathExtension:(NSString *)str; | |
12.@property(readonly, copy) NSString *stringByDeletingLastPathComponent; | |
13.@property(readonly, copy) NSString *stringByDeletingPathExtension; | |
14.@property(readonly, copy) NSString *stringByExpandingTildeInPath; | |
15.@property(readonly, copy) NSString *stringByResolvingSymlinksInPath; | |
16.@property(readonly, copy) NSString *stringByStandardizingPath; | |
17.- (NSArray |
|
URL相关 | 1.- (NSString *) stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters; |
2.@property(readonly, copy) NSString *stringByRemovingPercentEncoding; | |
语言标记 | 1.- (void) enumerateLinguisticTagsInRange:(NSRange)range scheme:(NSString *)tagScheme options:(NSLinguisticTaggerOptions)opts orthography:(NSOrthography *)orthography usingBlock:(void (^)(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop))block; |
2.- (NSArray |
看到这里可能大家晕了,还得吐槽下的markdown有的*都不显示,有时候显示。这些类属性或者方法,可以参考下图。
致谢
谢谢大家对我的持续关注。上面只是一个主要的框架展示,主要是让大家对这个框架有一个大体的认识,后面的会细说每个方法和属性的使用。
未完,待续~~~~,累了,休息了。