iOS开发之技能点互联网科技程序员

Objective-C字符串/数组/字典/集合常用方法整理

2016-03-25  本文已影响338人  一只不靠谱的猿_

字符串方法

获取字符串长度@property (readonly) NSUInteger length;

获取字符串中的字符- (unichar)characterAtIndex:(NSUInteger)index;

判断两个字符串是否相等- (BOOL)isEqualToString:(NSString *)aString;

字符串的比较- (NSComparisonResult)compare:(NSString *)string;

获取子字符串从下标num至最后- (NSString *)substringFromIndex:(NSUInteger)from;

获取子字符串从起始至下标num之前- (NSString *)substringToIndex:(NSUInteger)to;

获取子字符串于Range范围内- (NSString *)substringWithRange:(NSRange)range;

字符串拼接- (NSString *)stringByAppendingString:(NSString *)aString;

字符串替换- (NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement NS_AVAILABLE(10_5, 2_0);

字符串转为其他基本类型@property (readonly) double doubleValue;

                                   @property (readonly) float floatValue;

                                   @property (readonly) int intValue;

                                   @property (readonly) NSInteger integerValue NS_AVAILABLE(10_5, 2_0);

字母大小写控制@property (readonly, copy) NSString *uppercaseString;

                      @property (readonly, copy) NSString *lowercaseString;

                      @property (readonly, copy) NSString *capitalizedString;

判断是否以某个字符串开头/结尾- (BOOL)hasPrefix:(NSString *)str;

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

可变字符串方法

可变字符串拼接- (void)appendString:(NSString *)aString;

                      - (void)appendFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);

可变字符串插入- (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc;

删除字符串- (void)deleteCharactersInRange:(NSRange)range;

替换字符串- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString;

重置可变字符串- (void)setString:(NSString *)aString;

数组方法

元素个数@property (readonly) NSUInteger count;

获取数组中指定下标对应的元素- (ObjectType)objectAtIndex:(NSUInteger)index;

检测数组中是否包含某个对象- (BOOL)containsObject:(ObjectType)anObject;

获取数组某个元素对应的下标- (NSUInteger)indexOfObject:(ObjectType)anObject;

将字符串按一定的规律分割,并得到一个数组- (NSArray*)componentsSeparatedByString:(NSString *)separator;

将数组元素按照给定字符串穿插成字符串- (NSString *)componentsJoinedByString:(NSString *)separator;

可变数组方法

向数组添加一个对象(默认添加至最后)- (void)addObject:(ObjectType)anObject;

向数组指定位置添加对象- (void)insertObject:(ObjectType)anObject atIndex:(NSUInteger)index;

移除元素- (void)removeObjectAtIndex:(NSUInteger)index;

             - (void)removeLastObject;

             - (void)removeAllObjects;

替换指定下标的元素- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(ObjectType)anObject;

交换两个下标指定的对象- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;

字典方法

获取字典中键值对个数@property (readonly) NSUInteger count;

获取所有的键@property (readonly, copy) NSArray*allKeys;

获取所有的值@property (readonly, copy) NSArray*allValues;

获取指定key所对应的value - (nullable ObjectType)objectForKey:(KeyType)aKey;

可变字典方法

根据key值修改value,并且如果字典没有这个key那就会自动添加一个键值对

- (void)setObject:(ObjectType)anObject forKey:(KeyType)aKey;

移除指定key对应的键值对- (void)removeObjectForKey:(KeyType)aKey;

移除所有键值对(清空字典)- (void)removeAllObjects;

集合方法

获取集合中元素的个数@property (readonly) NSUInteger count;

获取集合中所有对象@property (readonly, copy) NSArray*allObjects;

随机获取集合中一个元素- (nullable ObjectType)anyObject;

检测集合中是否包含某个对象(是1否0)- (BOOL)containsObject:(ObjectType)anObject;

可变集合方法

添加一个对象- (void)addObject:(ObjectType)object;

移除一个对象- (void)removeObject:(ObjectType)object;

移除所有元素- (void)removeAllObjects;

上一篇 下一篇

猜你喜欢

热点阅读