1.1 直接创建----------------- NSString *str1
NSString *str1 = @"HaiFeng";
1.2 初始化方法创建------------initWithString
NSString *str2 = [[NSString alloc]initWithString:@"李白"];(有警告)
1.3 便利构造器创建------------stringWithString
NSString *str3 = [NSString stringWithString:@"李白"];(有告警)
2.1 format格式拼接-----------stringWithFormat
NSString *str4 = @"李白";
NSString *str5 = [NSString stringWithFormat:@"有没有叫%@的?",str4];
对于基本数据类型也可以使用到字符串当中
NSInteger a= 10;
NSString *str6 = [NSString stringWithFormat:@"小明今年%ld岁!",a];
2.2 将C中的字符串转化为OC对象--stringWithUTF8String
char symbol[10] = "Hello";//C语言中的字符串
NSString *str7 = [NSString stringWithUTF8String:symbol];
2.3 求字符串的长度------------length
NSString *str9 = [NSString stringWithFormat:@"我有多长"];
NSUInteger length = [str8 length];
2.4 判断字符串前缀------------hasPrefix
NSString *baiDuStr = @"http://www.baidu.com";
BOOL isStart = [baiDuStr hasPrefix:@"http://www"];
2.5 判断字符串后缀------------hasSuffix
BOOL isEnd = [baiDuStr hasSuffix:@".com"];
2.6 求字符串当中子字符串的范围--rangeOfString
NSString *rangeOfStr = @"我是乖宝宝";
NSRange range = [rangeOfStr rangeOfString:@"我是"];
NSrange是一个结构体, 成员有location和length 所以range.location 是字符串所给的子字符串范围的下标起始位置,如果子字符串有两个及两个以上的话,以遇到第一个为准;range.length 是子字符串的长度。
因为子字符串当中可以得到长度,所以如果不存在此字符串则length==0,所以我们通过Length的结果判断此时字符串当中有没有我们需要的子字符串(字符串当中的部分内容)
if (range.length!=0) {
NSLog(@"查找成功,存在此字符串!");
}else{
NSLog(@"查找不成功,不存在此字符串!");
}
2.7 字符串截取
2.7.1 开始到指定下标-1------substringToIndex
NSString *nameStr = @"Maoan LiZn333Zhe Taong444Yu Zhu4442Chao Chen43iFeng";
substringToIndex 字符串截取,从字符串开始一直截取到下标-1的位置,包含这个字符
NSString *receiveStr = [nameStr substringToIndex:9];
2.7.2 指定下标到字符串结束---substringFromIndex
NSRange nameRange= [nameStr rangeOfString:@"Chen"];
NSString *ChnHiFng = [nameStr substringFromIndex:nameRange.location];
2.7.3 范围截取-------------substringWithRange:NSMakeRange(3, 5)
NSString *str9= [nameStr substringWithRange:NSMakeRange(3, 5)];
2.8 拼接字符串
2.8.1 字符串对象拼接字符串---stringByAppendingString
NSString *oldStr = [NSString stringWithFormat:@"PengFei: I love you!"];
//为现有的字符串对象拼接字符串
NSString *newStr = [oldStr stringByAppendingString:@"ZhenZhe :Me too! Tonight ,come on baby!!"];
2.8.2 拼接格式串-----------stringByAppendingFormat
NSInteger totalAge = 79;
NSString *totalStr = [newStr stringByAppendingFormat:@"%ld",(long)totalAge];
2.9 替换字符串
2.9.1 整体替换(用后一个字符串替换前面的字符串)---stringByReplacingOccurrencesOfString: withString:
NSString *replaceStr = @"XiaoLng:JingTo,you JJFLy ,JianTao:I no,you can!";
//将JiangTao 换成 JunFeng 整体替换 用后一个字符串替换前面的字符串
NSString *newString = [replaceStr stringByReplacingOccurrencesOfString:@"JianTao" withString:@"JuFng"];
2.9.2 范围内整体替换(从起始位置也就是下标开始替换length个字符)-----stringByReplacingCharactersInRange:NSMakeRange(2, 5) withString:
NSString *twoNewStr = [newString stringByReplacingCharactersInRange:NSMakeRange(2, 5) withString:@"小笼包"];
2.10 比较字符串---------------compare
NSString *comStr1 = @"Hello!";
NSString *comStr2 = @"Hi!";
//接收一下两个字符串比较的结果
NSComparisonResult result = [comStr1 compare:comStr2];
//对于比较的结果是一个枚举 ,有三个值 升序 相等 降序
switch (result) {
case NSOrderedSame:
NSLog(@"两个字符串相等");
break;
case NSOrderedAscending:
NSLog(@"前一个字符串比第二个字符串小");
break;
case NSOrderedDescending:
NSLog(@"前一个字符串比第二个字符串大");
break;
default:
break;
}
2.11 对象转化为数值类型--------intValue
NSString *numberStr = @"123";//若想提取数据成功,则必须字符串对象里面只能出现数字
//对象转化为数值类型 intValue
int number = [numberStr intValue];
2.12 转成大写----------------uppercaseString
NSString *wordStr = @"Tonight i will close to you";
NSString *upperStr = [wordStr uppercaseString];
2.13 转成小写----------------lowercaseString
NSString *lowerStr = [upperStr lowercaseString];
2.14 将单词首字母转成大写------capitalizedString
NSString *capitalizedStr = [lowerStr capitalizedString];
2.1 通过初始化方法创建----------initWithCapacity
NSMutableString *mutStr = [[NSMutableString alloc]initWithCapacity:1];
2.2 便利构造器创建-------------stringWithCapacity
NSMutableString *mutStr = [NSMutableString stringWithCapacity:1];
2.3 拼接字符串-----------------appendFormat
[mutStr appendFormat:@"mutableString!"];
2.4 插入字符串----------------insertString atIndex:
[mutStr insertString:@"逗比" atIndex:2];
2.5 删除指定范围的字符串字符串---deleteCharactersInRange
NSRange rangeDelete = [mutStr rangeOfString:@"逗比"];
[mutStr deleteCharactersInRange:rangeDelete];
2.5.2 删除指定范围的字符串字符串-------deleteCharactersInRange:NSMakeRange(3, 3)
[mutStr deleteCharactersInRange:NSMakeRange(3, 3)];
NSArray *array = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
NSString *string = [array componentsJoinedByString:@","];//--分隔符
数值型转化为字符串型---------[NSString stringWithFormat:@"%d",i];
字符串转化为数字----------[str intValue];
数字转化为对象-------------- NSNumber numberWithInt:<#(int)#>
将汉字转化为UTF8编码格式------------
NSString *string = @"我是";
[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];