1.创建对象
两种方法
1.便利构造器(+号方法)
NSString *str1 = [NSString stringWithFormat:@"iPhone%d",5];
NSString *str3 = @"lanou";//储存在常量区
求字符串的长度
NSInteger length = [str2 length];
NSLog(@"%lu",length);
1.获取字符串中的某个字符
unichar charac = [str1 characterAtIndex:5];
NSLog(@"%c",charac);
2.判断是否以指定的字符串开头 或者结束
例如:判断一个字符串是否一http开头以.com结束
NSString *str4 = [[NSString alloc]initWithFormat:@"http://www.baidu.com"];
判断是否以http开头
BOOL isPrefix = [str4 hasPrefix:@"http"];
NSLog(@"isPrefix = %d",isPrefix);
判断是否以.com结尾
BOOL isSuffix = [str4 hasSuffix:@".com"];
NSLog(@"isSuffix = %d",isSuffix);
3.查找指定字符串的范围
NSRange range = [str4 rangeOfString:@"www"];
if (range.length == 0) {
NSLog(@"没有找到对应的字符串");
}else{
NSLog(@"location:%lu,length:%lu",rang.location,rang.length);
将NSRange类型的结构体变量转变为字符串对象
NSLog(@"%@",NSStringFromRange(range));
}
4.字符串截取(获取子字符串)
NSString *str5 = [NSString stringWithFormat:@"lanou is a big company"];
(1)获取给定下表之后的子串,包含指定下标对应的字符
NSString *subString1 = [str5 substringFromIndex:4];
NSLog(@"subString = %@",subString1);
(2)获取给定下表之前的子串,从下标为0开始,但是不包含指定下标的字符
NSString *subString2 = [str5 substringToIndex:4];
NSLog(@"subString = %@",subString2);
(3)获取指定范围内的字符串
NSString *subString3 = [str5 substringWithRange:NSMakeRange(0, 3)];
NSLog(@"%@",subString3);
5.字符串拼接
NSString *str1 = [NSString stringWithFormat:@"lanou"];
lanou后拼接 henan9ban //format 可以添加%d ,9
NSString *newstr2 = [str1 stringByAppendingFormat:@"%@",newstr1];
NSLog(@"%@",newstr2);
6.替换字符串
NSString *str2 = [NSString stringWithFormat:@"lanououououisbigcompany"];
(1)将ou替换成牛逼
NSString *newstr = [str2 stringByReplacingOccurrencesOfString:@"ou" withString:@"NB"];
NSLog(@"%@",newstr);
(2).只将一个ou替换成NB
NSString *newstr3 = [str2 stringByReplacingCharactersInRange:NSMakeRange(3, 8) withString:@"NB"];
NSLog(@"%@",newstr3);
9.字符串比较
NSComparisonResult result = [@"1111" compare:@"2222"];
NSLog(@"result = %ld",result);
enum {
NSOrderedAscending = -1,升序
NSOrderedSame, 0 相等
NSOrderedDescending 1 降序
};
10.字符串和数值转换
(1).@"123" 将字符串123转换成整数123
从第一个字符开始,如果在遇到除空格之外的字符时,将前面的空格忽略掉,继续往下找,读取数字,直到遇到非数字时结束
int value = [@"12a3" intValue];
NSLog(@"%lu",[@" 123" length]);
NSLog(@"%d",value);
(2).将数字转换为字符串对象
NSString *str = [NSString stringWithFormat:@"%f",5.2];
11.字符串大小写转化
(1)将@"lan OU"全部转化为大写
NSString *upperString = [@"lan OU" uppercaseString];
NSLog(@"uppString = %@",upperString);
(2)将@"lan OU"全部转化为小写
NSString *lowerString = [@"lan OU" lowercaseString];
NSLog(@"lowerString = %@",lowerString);
(3)将@"lan OU"每个单词的首字母大写
NSString *capital = [@"lan OU" capitalizedString];
NSLog(@"capital = %@",capital);
NSMutableString *str = [NSMutableString stringWithFormat:@"lanouhenan"];
对于可变字符串的增删改方法,都是在元字符串的基础上进行修改的,所以修改之后元字符串内容发生改变
1.在原字符串str基础上拼接Frank
[str appendFormat:@"Frank"];
NSLog(@"%@",str);
2.在字符串str基础上拼接Frank
[str appendFormat:@"Frank"];
NSLog(@"%@",str);
3.删除字符串,在元字符串基础上删除henan.
[str deleteCharactersInRange:NSMakeRange(0, 5)];
NSLog(@"%@",str);
4.插入字符串
[str insertString:@"henan" atIndex:5];
NSLog(@"%@",str);
5.替换字符串
[str replaceCharactersInRange:NSMakeRange(10, 5) withString:@"Duck"];
NSLog(@"%@",str);
两种方法
1.便利构造器(+号方法)
NSString *str1 = [NSString stringWithFormat:@"iPhone%d",5];
NSLog(@"%@",str1);
2.alloc + 初始化方法
NSString *str2 = [[NSString alloc]initWithFormat:@"iPhone%d",4];
NSLog(@"%@",str2);
3.不常用的字符串NSString *str3 = @"lanou";//储存在常量区
求字符串的长度
NSInteger length = [str2 length];
NSLog(@"%lu",length);
1.获取字符串中的某个字符
unichar charac = [str1 characterAtIndex:5];
NSLog(@"%c",charac);
2.判断是否以指定的字符串开头 或者结束
例如:判断一个字符串是否一http开头以.com结束
NSString *str4 = [[NSString alloc]initWithFormat:@"http://www.baidu.com"];
判断是否以http开头
BOOL isPrefix = [str4 hasPrefix:@"http"];
NSLog(@"isPrefix = %d",isPrefix);
判断是否以.com结尾
BOOL isSuffix = [str4 hasSuffix:@".com"];
NSLog(@"isSuffix = %d",isSuffix);
3.查找指定字符串的范围
NSRange range = [str4 rangeOfString:@"www"];
if (range.length == 0) {
NSLog(@"没有找到对应的字符串");
}else{
NSLog(@"location:%lu,length:%lu",rang.location,rang.length);
将NSRange类型的结构体变量转变为字符串对象
NSLog(@"%@",NSStringFromRange(range));
}
4.字符串截取(获取子字符串)
NSString *str5 = [NSString stringWithFormat:@"lanou is a big company"];
(1)获取给定下表之后的子串,包含指定下标对应的字符
NSString *subString1 = [str5 substringFromIndex:4];
NSLog(@"subString = %@",subString1);
(2)获取给定下表之前的子串,从下标为0开始,但是不包含指定下标的字符
NSString *subString2 = [str5 substringToIndex:4];
NSLog(@"subString = %@",subString2);
(3)获取指定范围内的字符串
NSString *subString3 = [str5 substringWithRange:NSMakeRange(0, 3)];
NSLog(@"%@",subString3);
5.字符串拼接
NSString *str1 = [NSString stringWithFormat:@"lanou"];
lanou后拼接 henan
NSString *newstr1 = [str1 stringByAppendingString:@"henan"];
NSLog(@"%@",newstr1);lanou后拼接 henan9ban //format 可以添加%d ,9
NSString *newstr2 = [str1 stringByAppendingFormat:@"%@",newstr1];
NSLog(@"%@",newstr2);
6.替换字符串
NSString *str2 = [NSString stringWithFormat:@"lanououououisbigcompany"];
(1)将ou替换成牛逼
NSString *newstr = [str2 stringByReplacingOccurrencesOfString:@"ou" withString:@"NB"];
NSLog(@"%@",newstr);
(2).只将一个ou替换成NB
NSString *newstr3 = [str2 stringByReplacingCharactersInRange:NSMakeRange(3, 8) withString:@"NB"];
NSLog(@"%@",newstr3);
9.字符串比较
NSComparisonResult result = [@"1111" compare:@"2222"];
NSLog(@"result = %ld",result);
enum {
NSOrderedAscending = -1,升序
NSOrderedSame, 0 相等
NSOrderedDescending 1 降序
};
10.字符串和数值转换
(1).@"123" 将字符串123转换成整数123
从第一个字符开始,如果在遇到除空格之外的字符时,将前面的空格忽略掉,继续往下找,读取数字,直到遇到非数字时结束
int value = [@"12a3" intValue];
NSLog(@"%lu",[@" 123" length]);
NSLog(@"%d",value);
(2).将数字转换为字符串对象
NSString *str = [NSString stringWithFormat:@"%f",5.2];
11.字符串大小写转化
(1)将@"lan OU"全部转化为大写
NSString *upperString = [@"lan OU" uppercaseString];
NSLog(@"uppString = %@",upperString);
(2)将@"lan OU"全部转化为小写
NSString *lowerString = [@"lan OU" lowercaseString];
NSLog(@"lowerString = %@",lowerString);
(3)将@"lan OU"每个单词的首字母大写
NSString *capital = [@"lan OU" capitalizedString];
NSLog(@"capital = %@",capital);
NSMutableString *str = [NSMutableString stringWithFormat:@"lanouhenan"];
对于可变字符串的增删改方法,都是在元字符串的基础上进行修改的,所以修改之后元字符串内容发生改变
1.在原字符串str基础上拼接Frank
[str appendFormat:@"Frank"];
NSLog(@"%@",str);
2.在字符串str基础上拼接Frank
[str appendFormat:@"Frank"];
NSLog(@"%@",str);
3.删除字符串,在元字符串基础上删除henan.
[str deleteCharactersInRange:NSMakeRange(0, 5)];
NSLog(@"%@",str);
4.插入字符串
[str insertString:@"henan" atIndex:5];
NSLog(@"%@",str);
5.替换字符串
[str replaceCharactersInRange:NSMakeRange(10, 5) withString:@"Duck"];
NSLog(@"%@",str);