iOS开发OC基础之NSString篇

创建 NSString 三种方法
 常量>>       @“”   
 格式化>>     initWithFormat
 格式化>>     initWithString
NSString的属性
返回字符串的长度
@property (readonly) NSUInteger length; 
返回对应的类型
@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
大小写转换
@property (readonly, copy) NSString *uppercaseString;
@property (readonly, copy) NSString *lowercaseString;
@property (readonly, copy) NSString *capitalizedString;
@property (readonly, copy) NSString *localizedUppercaseString
@property (readonly, copy) NSString *localizedLowercaseString 
@property (readonly, copy) NSString *localizedCapitalizedString
字符串UTF8编码
@property (nullable, readonly) __strong const char *UTF8String
NSString常用方法
- (unichar)characterAtIndex:(NSUInteger)index; 返回index位置对应的字符
字符串截取
- (NSString *)substringFromIndex:(NSUInteger)from; 从指定位置from开始(包括指定位置的字符)到尾部
- (NSString *)substringToIndex:(NSUInteger)to;  从字符串的开头一直截取到指定的位置,但不包括该位置的字符
- (NSString *)substringWithRange:(NSRange)range;  按照所给出的NSRange从字符串中截取子串
字符串比较
- (NSComparisonResult)compare:(NSString *)string; 这个方法可以用来比较两个字符串内容的大小
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask; 忽略大小写进行比较,返回值与compare:一致
- (BOOL)isEqualToString:(NSString *)aString; 是否等于某字符串
字符串搜索
- (BOOL)hasPrefix:(NSString *)str; 判断是否以什么开头(可以用来判断协议头)
- (BOOL)hasSuffix:(NSString *)str; 判断是否以什么结尾
- (BOOL)containsString:(NSString *)str; 检索字符串中是否存在某一字符串
- (NSRange)rangeOfString:(NSString *)searchString; 查找字符串中是否某一个字符串返回该字符串的范围
字符串拼接
- (NSString *)stringByAppendingString:(NSString *)aString;
- (NSString *)stringByAppendingFormat:(NSString *)format;
字符串大小写处理
- (NSString *)uppercaseStringWithLocale:(nullable NSLocale *)locale;
- (NSString *)lowercaseStringWithLocale:(nullable NSLocale *)locale; 
- (NSString *)capitalizedStringWithLocale:(nullable NSLocale *)locale; 首字母变大写,其他字母都变小写
字符串编码
- (nullable NSData *)dataUsingEncoding:(NSStringEncoding)encoding; 字符串---->NSData数据
- (nullable instancetype)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding; NSData---->字符串
字符串分割
- (NSArray *)componentsSeparatedByString:(NSString *)separator; 分割字符串
- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator; 分割字符串,返回数组
字符串替换
-  (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set; 替换首尾
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement ; 用replacement替换字符串中的指定字符串
字符串写入
通过URL写入文件中(BOOL:  YES 没写完不生成   no 没写完也生成)(重复写入同一文件会覆盖掉上一次的内容;
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error; 
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile   encoding:(NSStringEncoding)enc error:(NSError **)error; 通过全路径写入文件中
- (nullable instancetype)initWithUTF8String:(const char *)nullTerminatedCString; C语言字符串---->OC字符串
+ (nullable instancetype)stringWithUTF8String:(const char *)nullTerminatedCString; C语言字符串---->OC字符串(直接使用UTF-8编码)
+ (nullable instancetype)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc; C语言字符串---->OC字符串
字符串读取   如果读写错误会把错误写入error指针指向的地址中
+ (nullable instancetype)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error; 从URL中读取文件
+ (nullable instancetype)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error; 从文件中读取
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile; 通过URL写入文件中
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically; 通过全路径写入文件中
- (void)getCharacters:(unichar *)buffer; 获取对应地址缓存的字节 ,这个方法不安全 ,最好使用-getCharacters:range:
NSString操作方法
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString; 使用aString替换range范围内的字符串
NSMutableString (NSMutableStringExtensionMethods)    分类
- (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc; 在loc这个位置中插入aString
- (void)deleteCharactersInRange:(NSRange)range; 删除range范围内的字符串
- (void)appendString:(NSString *)aString; 拼接aString到最后面
- (void)appendFormat:(NSString *)format, ... 拼接一段格式化字符串到最后面
NSString扩展方法
NSString(NSStringDrawing)    分类
- (CGSize)sizeWithAttributes:(nullable NSDictionary *)attires; 计算文本显示需要的区域--只能计算单行 
- (void)drawAtPoint:(CGPoint)point 
withAttributes:(nullable NSDictionary *)attires;  绘制文字--不会自动换行--文字属性
- (void)drawInRect:(CGRect)rect 
  withAttributes:(nullable NSDictionary *)attires; 绘制文字--自动换行--文字属性
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options  attributes:(nullable NSDictionary *)attributes  context:(nullable NSStringDrawingContext *)context;  
计算给定文本字符串所占区域大小,返回值是一个x,y=0   w, h计算好的 CGRect字符串方法. NSDictionary  用于指定字体的相关属性的字典,从UIKit框架中得第一个头文件找NSAttributedString.h
其他类对NSString的扩展
NSURL  统一资源定位符对NSString的扩展---->>NSString (NSURLUtilities)  字符串转码
- (nullable NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc ; 对字符串进行转码(中文转码)(百分号转码)
@property (nullable, readonly, copy) NSString *stringByRemovingPercentEncoding; 字符串反编码

NSPathUtilities 路径 对NSString的扩展---->> 对NSString 路径操作
@property (getter=isAbsolutePath, readonly) BOOL absolutePath; 是否为绝对路径
@property (readonly, copy) NSString *lastPathComponent; 获得最后一个目录
@property (readonly, copy) NSString *stringByDeletingLastPathComponent;删除最后一个目录
@property (readonly, copy) NSString *pathExtension; 获得拓展名
@property (readonly, copy) NSString *stringByDeletingPathExtension; 删除尾部的拓展名
- (NSString *)stringByAppendingPathComponent:(NSString *)str; 在路径的后面拼接一个目录 (会自动拼接"/")
- (nullable NSString *)stringByAppendingPathExtension:(NSString *)str; 在尾部添加一个拓展名

NSStringDrawing   绘制文字对NSString的扩展---->>NSString 的文字绘制
NSString(NSStringDrawing)    分类
- (CGSize)sizeWithAttributes:(nullable NSDictionary *)attires; 计算文本显示需要的区域--只能计算单行 
- (void)drawAtPoint:(CGPoint)point withAttributes:(nullable NSDictionary *)attires; 绘制文字--不会自动换行--文字属性
- (void)drawInRect:(CGRect)rect  withAttributes:(nullable NSDictionary *)attires; 绘制文字--自动换行--文字属性
NSString (NSExtendedStringDrawing)   分类
 - (CGRect)boundingRectWithSize:(CGSize)size  options:(NSStringDrawingOptions)options  attributes:(nullable NSDictionary *)attributes  context:(nullableNSStringDrawingContext *)context; 计算给定文本字符串所占区域大小,返回值是一个x,y=0   w, h计算好的 CGRect

NSString+Hash分类   对NSString的扩展----->>NSString MD5加密
@property (readonly) NSString *md5String; 返回MD5加密字符串

你可能感兴趣的:(iOS开发OC基础之NSString篇)