iOS基础:String常用方法

一、NSString常用方法


//将c字符串转为oc字符串

+ (nullableinstancetype)stringWithCString:(constchar *)cString encoding:(NSStringEncoding)enc;

+ (nullableinstancetype)stringWithUTF8String:(constchar *)nullTerminatedCString;

//将oc字符串转为c字符串

char * cStr = [str3UTF8String];

//查找字符串

- (NSRange)rangeOfString:(NSString *)searchString;

//获取子字符串

- (NSString *)substringFromIndex:(NSUInteger)from;

- (NSString *)substringToIndex:(NSUInteger)to;

- (NSString *)substringWithRange:(NSRange)range;

//比较字符串

- (NSComparisonResult)compare:(NSString *)string;

NSOrderedAscending==> 小于

NSOrderedDescending ==> 大于

NSOrderedSame       ==> 相等

- (BOOL)isEqualToString:(NSString *)aString;

//字符串前后缀

- (BOOL)hasPrefix:(NSString *)str;

- (BOOL)hasSuffix:(NSString *)str;

//字符串与字符

-(unichar)characterAtIndex:(NSUInteger)index;

NSUInteger length = [strlength];

//字符串分隔与拼接

- (NSArray<NSString *> *)componentsSeparatedByString:(NSString *)separator;

- (NSArray<NSString *> *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator NS_AVAILABLE(10_5,2_0);


二、NSMutableSting常用的方法

//覆盖

- (void)setString:(NSString *)aString;

//插入

- (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc;

//增   

- (void)appendString:(NSString *)aString;

- (void)appendFormat:(NSString *)format, ... 

//删

- (void)deleteCharactersInRange:(NSRange)range;

//改

- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString; 

//替换

urlStr = [urlStr stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];













你可能感兴趣的:(ios)