- iOS官方文档 Foundation篇---Strings
- iOS官方文档 Foundation篇---Strings wi
- iOS官方文档 Foundation篇---Numbers
- iOS官方文档 Foundation篇---NSDictiona
- iOS官方文档 Foundation篇---NSDateInte
- iOS官方文档 Foundation篇---NSDateComp
- iOS官方文档 Foundation篇---NSDate
- iOS官方文档 Foundation篇---NSHashTabl
- iOS官方文档 Foundation篇---NSPointerA
- iOS官方文档 Foundation篇---NSMapTable
如果一个人只有勇敢而没有机智,那么勇敢就会变成幼稚。
————卜相机关
NSString
静态,纯文本Unicode字符串对象,用于处理字符串;其中包括用于比较,寻找方法和修改字符串等方法;
#创建和初始化字符串
//普通创建
NSString *string = @"qwerty";
NSString *nullStr = [[NSString alloc]init];
NSString *initString = [[NSString alloc]initWithString:string];
NSString *formatString = [[NSString alloc]initWithFormat:@"%@",string];
//通过文件创建
NSString *path = [[NSBundle mainBundle]pathForResource:@"astring.text"ofType:nil];
NSString *pathString = [[NSString alloc]initWithContentsOfFile:path];
NSString *pathString = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSString *pathString = [[NSString alloc]initWithContentsOfFile:path usedEncoding:NSUTF8StringEncoding error:nil];//报错
NSURL *url = [NSURL fileURLWithPath:path];
NSString *urlString = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSString *urlString = [[NSString alloc]initWithContentsOfURL:url usedEncoding:NSUTF8StringEncoding error:nil];//报错
//通过字符集创建
char *chars = "123456";
NSString *charString = [[NSString alloc]initWithCharactersNoCopy:chars length:strlen(chars) freeWhenDone:YES];
NSString *charString = [[NSString alloc]initWithCharacters:chars length:strlen(chars)];
NSString *charString = [[NSString alloc]initWithUTF8String:chars];
NSString *charString = [[NSString alloc]initWithCString:chars encoding:NSUTF8StringEncoding];
//通过data创建
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *dataString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//根据语言环境创建
NSLocale *locale = [[NSLocale alloc]
initWithLocaleIdentifier:@"fr_FR"];
NSString *localeString = [[NSString alloc]initWithFormat:string locale:locale];
#截取字符串
//获取字符串长度
NSUInteger length = string.length;
//获取字符
unichar ch = [string characterAtIndex:0];
//从给定索引处的字符到结尾,包含接收者的字符;
NSString *fromStr = [string substringFromIndex:2];//erty
//到给定索引,但不包括给定索引处的字符。
NSString *tostr = [string substringToIndex:2];//qw
//其中包含位于给定范围内的接收者的字符
NSString *rangeStr = [string substringWithRange:NSMakeRange(1, 2)];//we
#字符串比较
NSString *string = @"qwerty";
NSString *equalStr = @"QWERTY";
//区分大小写
BOOL isEqual = [string compare:equalStr] == NSOrderedSame;//NO
//不区分大小写
BOOL isEqual = [string caseInsensitiveCompare:equalStr] == NSOrderedSame;//YES
//根据条件比较
/*
NSCaseInsensitiveSearch //不区分大小写比较
NSLiteralSearch //逐字节比较 区分大小写
NSBackwardsSearch //从字符串末尾开始比较
NSAnchoredSearch //搜索限制范围的字符串
NSNumericSearch //按照字符串里的数字为依据比较
NSDiacriticInsensitiveSearch //忽略变音符号的比较
NSWidthInsensitiveSearch //忽略具有全宽和半宽形式的字符的宽度差异,比较出结果
NSForcedOrderingSearch //忽略不区分大小写比较的选项,并强制返回 NSOrderedAscending 或者 NSOrderedDescending
NSRegularExpressionSearch //只能应用于 rangeOfString:..., stringByReplacingOccurrencesOfString:...和 replaceOccurrencesOfString:... 方法。使用通用兼容的比较方法,如果设置此项,可以去掉 NSCaseInsensitiveSearch 和 NSAnchoredSearch
*/
BOOL isEqual = [string compare:equalStr options:NSCaseInsensitiveSearch] == NSOrderedSame;//YES
//根据条件范围比较
BOOL isEqual = [string compare:equalStr options:NSCaseInsensitiveSearch range:NSMakeRange(0, string.length)] == NSOrderedSame;//YES
//使用本地化字符串和给定字符串比较
BOOL isEqual = [string localizedCompare:equalStr] == NSOrderedSame;//NO
//不区分大小写本地化字符串比较
BOOL isEqual = [string localizedCaseInsensitiveCompare:equalStr] == NSOrderedSame;//YES
//系统排序之后的比较
BOOL isEqual = [string localizedStandardCompare:equalStr] == NSOrderedSame;//NO
//字符串是否相同
BOOL isEqual = [string isEqualToString:equalStr];//NO
#字符串搜索排序
//字符是否与字符串的开头字符相同
BOOL isPrefix = [string hasPrefix:@"q"];//YES
//字符是否与字符串的结尾字符相同
BOOL isSuffix = [string hasSuffix:@"y"];//YES
//根据条件获取相同前缀
NSString *newString = [string commonPrefixWithString:@"QWERTYUIOP" options:NSCaseInsensitiveSearch];//@"qwerty"
//是否包含给定字符串。
BOOL isContain = [string containsString:equalStr];//NO
//不区分大小写是否包含给定字符串
BOOL iscon1 = [string localizedCaseInsensitiveContainsString:equalStr];//YES
//查找并返回字符串中第一次出现的给定字符串的范围。
NSRange range = [string rangeOfString:@"q"];//{0,1}
//根据给定的选项查找并返回字符串中给定字符串第一次出现的范围
NSRange range = [string rangeOfString:@"Q"options:NSCaseInsensitiveSearch];//{0,1}
//根据给定的选项和范围查找并返回字符串中给定字符串第一次出现的范围
NSRange range4 = [string rangeOfString:@"Q" options:NSCaseInsensitiveSearch range:NSMakeRange(0, string.length)];//{0,1}
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"q"];
//查找并返回给定字符集中第一个字符的字符串中的范围,set不能为空
NSRange range = [string rangeOfCharacterFromSet:set];//{0,1}
//使用给定选项从给定字符集中查找并返回第一个字符的字符串中的范围
NSRange range = [string rangeOfCharacterFromSet:set options:NSLiteralSearch];//{0,1}
//使用给定选项和范围从给定字符集中查找并返回第一个字符的字符串中的范围
NSRange range = [string rangeOfCharacterFromSet:set1 options:NSLiteralSearch range:NSMakeRange(0, string.length)];
//返回位于给定索引处的组合字符串的接收器中的范围。
NSRange range6 = [string rangeOfComposedCharacterSequenceAtIndex:0];//{0,1}
//追加拼接字符串
NSString *appendStr = [string stringByAppendingString:equalStr];////qwertyQWERTY
NSString *appendString = [string stringByAppendingFormat:@"%@",equalStr];//qwertyQWERTY
#字符串转化
NSString *num = @"1234.5678";
double nums = num.doubleValue;//1234.5678
float nums = num.floatValue;//1234.5678
int nums = num.intValue;//1234
NSInteger nums = num.integerValue;//1234
BOOL nums = num.boolValue;//YES
#字符串大小写
//转大写
NSString *newSt = string.uppercaseString;//QWERTY
//转小写
NSString *newSt1 = string.lowercaseString;//qwerty
//字符串的大写表示
NSString *newSt2 = string.capitalizedString;//Qwerty
#字符串查询行、句、词
//遍历字符串中的所有行
[string enumerateLinesUsingBlock:^(NSString * _Nonnull
line, BOOL * _Nonnull stop) {
NSLog(@"%@",line);
}];
/*
NSStringEnumerationByLines //行
NSStringEnumerationByParagraphs //段落
NSStringEnumerationByComposedCharacterSequences //字符
NSStringEnumerationByWords //单字
NSStringEnumerationBySentences //句子
NSStringEnumerationReverse //反向遍历
NSStringEnumerationSubstringNotRequired //不需要子字符串
NSStringEnumerationLocalized //本地化
*/
//遍历指定字符串范围内指定类型的子字符串
[string enumerateSubstringsInRange:NSMakeRange(0, string.length) options:NSStringEnumerationByLines usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
}];
#字符串编码
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
//使用指定格式编码字符串,允许在编码过程中更改或删除字符
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
//示接收器是否可以转换为给定的编码而不会丢失信息
BOOL isEncoding = [string canBeConvertedToEncoding:NSUTF8StringEncoding];
#分割字符串
NSArray *array = [string
componentsSeparatedByString:@"e"];//(qw,rty)
NSArray *array = [string componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"e"]];//(qw,rty)
//两端移除给定字符集中包含的字符而生成的新字符串
NSString *newString = [string
stringByTrimmingCharactersInSet:[NSCharacterSet
characterSetWithCharactersInString:@"qy"]];//wert
//通过从末尾删除字符,填充新的字符串(newLength接收器的新长度;padString用于扩展接收器的字符串; padIndex从中开始填充的索引)
NSString *newString = [string stringByPaddingToLength:10 withString:equalStr startingAtIndex:1];//qwertyWERT
//接收器中所有出现的目标字符串都被另一个给定字符串替换。
NSString *newString = [string stringByReplacingOccurrencesOfString:@"q" withString:@"a"];//awerty
//接收器指定范围内所有出现的目标字符串都被另一个给定字符串替换。
NSString *newString = [string stringByReplacingOccurrencesOfString:@"w" withString:@"1" options:NSBackwardsSearch range:NSMakeRange(0, 3)];//q1erty
//接收器指定范围内的字符由给定字符串替换。
NSString *newString = [string stringByReplacingCharactersInRange:NSMakeRange(0, 4) withString:@"23"];//23ty
//无符号整数,可用作哈希表地址
NSInteger hash = string.hash;//10007558234663730
NSMutableString
动态纯文本Unicode字符串对象,可变的字符串,其内容可以被编辑,继承自NSSting;
#创建和初始化字符串
NSString *str = @"qwerty";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSMutableString *string = [[NSMutableString alloc]initWithString:@"qwerty"];//qwerty
NSMutableString *string = [[NSMutableString alloc]initWithFormat:@"%@",@"qwerty"];//qwerty
NSMutableString *string = [[NSMutableString alloc]initWithData:data encoding:NSUTF8StringEncoding];//qwerty
NSMutableString *mulString = [[NSMutableString alloc]initWithCapacity:12];
#操作字符串
//追加字符串
[mulString appendFormat:@"%@",str];//qwerty
[mulString appendString:str];//qwertyqwerty
NSMutableString *mulStr = [[NSMutableString alloc]initWithString:@"qwerty"];
//替换指定位置字符串
[mulStr replaceCharactersInRange:NSMakeRange(0, 1) withString:@"Q"];//Qwerty
//在给定位置插入给定字符串
[mulStr insertString:@"8" atIndex:3];//Qwe8rty
//删除指定范围内的字符串
[mulStr deleteCharactersInRange:NSMakeRange(3,1)];//Qwerty
//替换字符串
[mulStr setString:@"ytrewq"];//ytrewq
//替换范围内所有出现的字符
NSMutableString *mulString = [[NSMutableString alloc]initWithString:@"qwertyQWERTYytrewqYTREWQ"];
NSUInteger num = [mulString replaceOccurrencesOfString:@"q" withString:@"Q" options:NSLiteralSearch range:NSMakeRange(0, mulSt.length)];//QwertyQWERTYytrewQYTREWQ num = 2
网友评论