NSString篇:
1.字符串连接
1 NSString *beijing = @"北京";
2 NSString *welcome = [beijing stringByAppendingString:@" 欢迎你 "];//字符串连接
3 NSLog(@"%@",welcome);
//两种效果相同的字符串连接
NSString *str22 = [str20 stringByAppendingFormat:@"%@",str21];
NSString *str23 = [str20 stringByAppendingString:str21];
2.字符串长度
1 NSLog(@"%zi",welcome.length);//字符串的长度
3.字符串比较
注意比较的是内容,此处并未比较地址
1 BOOL flag = [beijing isEqualToString:@"北京"];//字符串比较
2 NSLog(@"%d",flag);//布尔类型 1相等 0不相等
比较(地址+内容)
1 NSString *sstring1 = [[NSString alloc] initWithFormat:@"123"];
2 NSString *sstring2 = [[NSString alloc] initWithFormat:@"123"];
3 if(sstring1 != sstring2)
4 NSLog(@"地址不相等");
5 else NSLog(@"地址相等");
NSLog(@"%p,%p"sstring1,sstring2);
4.字符串向数据的转换
1 NSString *number = @"123456",*floatNumber = @"1233.45";
2 float aa = [floatNumber floatValue];// 字符串转换成浮点型
3 int a = [number intValue];//字符串转换成整型
4 NSLog(@"%d,%f",a,aa);
5.字符串本身元素的索引
1 NSString *seachStr = @"12345.doc";
2 BOOL f = [seachStr hasPrefix:@"123"];//判断A字符串是否以B字符串为开头
3 BOOL f1 = [seachStr hasSuffix:@".doc"];//判断A字符串是否以B字符串结尾
4 NSRange range = [seachStr rangeOfString:@"234"];//搜索B字符串在A字符串中的范围
5 //库中的NSRange(包含了位置和长度的结构体)
6 // typedef struct _NSRange {
7 // NSUInteger location;
8 // NSUInteger length;
9 // } NSRange;
10 NSLog(@"%d,%d,%lu,%lu",f,f1,(range.location),(range.length));//位置 长度 (unsigned long)
11
12 if (range.location == NSNotFound)
13 {
14 NSLog(@"没找到");
15 }
6.字符串的裁剪
1 NSString *subResource = @"abcde";
2 NSString *subStr = [subResource substringFromIndex:3];//从位置1开始剪切字符串,包含1
3 NSLog(@"%@",subStr);
4
5 NSString *subStr2 = [subResource substringToIndex:3];//剪切到位置3,不包含3,留下3
6 NSLog(@"%@",subStr2);
7
8 NSString *subResource = @"abcde";
9 NSRange range1 = NSMakeRange(1, 2);
10 // range1.location = 1;//位置
11 // range1.length = 2;//长度
12 //等价
13 //库中的NSMakeRange
14 // NS_INLINE NSRange NSMakeRange(NSUInteger loc, NSUInteger len) {
15 // NSRange r;
16 // r.location = loc;
17 // r.length = len;
18 // return r;
19 // }
20 NSString *subStr3 = [subResource substringWithRange:range1];//剪切给定范围的字符串
21 //compare方法:1.升序:前 < 后(-1),2.相等0 3.降序:前 > 后(1)
22 NSLog(@"%@",subStr3);
7.字符串的比较
1 NSLog(@"%d",(NSComparisonResult result = [@"ABC" compare:@"abc"]));
2 NSComparisonResult/*枚举类型*/ result = [@"CBA" compare:@"abc" ] ;
3 NSLog(@"%ld",result);// NSOrderedAscending = -1L,存储类型L
4 /*typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending};*/
// NSComparisonResult result = [@“” caseInsensitiveCompare:@“”];
不区分大小写的比较
8.字符串元素插入、剔除、替换
1 NSString *string = @"Hello World and Sunshine";
2 NSString *string1= [string stringByReplacingOccurrencesOfS tring:@"World" withString:@"iBokanWisdom"];
3 NSLog(@"替换之前%@替换之后%@",string,string1);
1 #import <Foundation/Foundation.h>
2
3 int main(int argc, const char * argv[])
4 {
5 @autoreleasepool
6 {
7 NSString *string = @"Hello World and Sunshine";
8 NSString *string1= [string stringByReplacingOccurrencesOfString:@"World" withString:@"iBokanWisdom"];
9 NSLog(@"替换之前%@替换之后%@",string,string1);
10
11 NSRange range = [string rangeOfString:@"World"];//搜索World字符串在Hello World and Sunshine字符串中的范围
12 NSString *string3 = [string stringByReplacingCharactersInRange:range withString:@"iBokanWisdom"];//某范围插入字符串
13 NSLog(@"%@",string3);
14 NSArray *array = [string componentsSeparatedByString:@" " ];//字符串转换为数组
15 //array = @[@"Hello",@"World",@"and",@"Sunshine"];
16 NSMutableArray *mutableArray = [array mutableCopy];//数组转换
17 [mutableArray replaceObjectAtIndex:1 withObject:@"iBokanWisdom"];//iBokanWisdom替换World
18 // [NSMutableArray componentsJoinedByString:@" “];
19 NSString *string2 = [mutableArray componentsJoinedByString:@" "];//数组转换为字符串
20 NSLog(@"%@",string2);
21
22 }
23 return 0;
24 }
// NSString构造方法:
//构造方法,语法跟NSNumber,NSArray差不多
stringWithString:
stringWithFormat:@""
//比较
isEqualTo..
compare
//获取X之前
sub..ToIndex
//截取X到尾
sub..FromIndex
//查找
rangeOf..
NSMutableString篇(注意:可变字符串与不可变字符串的相同操作方法是不同的)
1 NSMutableString *mutableString = [[NSMutableString alloc] initWithCapacity/*容量*/:0];//可变字符串初始化
2 //首先分配0个存储空间,当字符串长度超出范围后,再去开辟新的存储空间
3 [mutableString setString:@"123456"];
4 NSString *sstring;//不可变字符串初始化
5 sstring = @"123456";//不可变字符串赋值
6 [mutableString appendString:@"789"];//可变字符串的追加方法,不产生新的字符串,直接改变原有的字符串
7 NSString *welcome = [sstring stringByAppendingString:@" 欢迎你 "];//不可变字符串的追加方法,要产生新的字符串
8 NSLog(@"%@--%@",mutableString,welcome);
9 //总结:可变字符串只有初始化并且说明长度后才可以对其操作,否则为空
//插入字符串
insertString
//追加字符串
appendString
//替换字符串
deleteCharactersInRange:withString