------------1.字符串的创建
——————————
//1 快速创建
NSString *str1 = @“这是一个字符串!”;
//2 alloc创建的字符串
NSString *str2 = [[NSString alloc] initWithString:@“通过alloc创建的字符串"];
//3通过格式化符创建
int a = 100;
NSString *str3 = [[NSString alloc] initWithFormat:@“通过alloc创建,数值是:%d”,a];
//4 通过格式化符创建
int a = 100;
NSString *str4 = [NSString stringWithFormat:@“通过类方法创建,数值是:%d”,a];
NSString *s1 = @“ab”;
NSString *s2 = @“cd”;
NSString *string1 = [NSString stringWithFormat:@“%@,%@”,s1,s2];
------------2.字符串的比较——————————
NSString *s1 = [NSString stringWithFormat:@“字符串"];
NSString *s2 = [NSString stringWithFormat:@“字符串"];
if(s1==s2){
NSLog(@“s1==s2");
}
2比较的是字符串的内容
NSString *s1 = [NSString stringWithFormat:@“字符串"];
NSString *s2 = [NSString stringWithFormat:@“字符串"];
BOOL isSame = [s1 isEqualToString:s2];
if(isSame){
NSLog(@“字符串内容一样");
}
二 比较字符串的大小
NSString *s1 = [NSString stringWithFormat:@“a"];
NSString *s2 = [NSString stringWithFormat:@“b"];
//比较大小(比较的地址)
NSComparisonResult result = [s1 caseInsensitiveCompare:s2];
if(result==NSOrderedAscending){
NSLog(@“升序:a
}
else if(result==NSOrderedSame){
NSLog(@“内容相同");
}
else if(result==NSOrderedDescending){
NSLog(@“降序 a>b");
}
三 字符串的长度
NSString *s = @“abc”;
//字符串长度
unsigned int len = [s length];
------------3.字符串的转换——————————
//大小写转换
NSString *hello = @“hello WORLD”;
NSLog(@“%@“,[hello uppercaseString]);//全部大写
NSLog(@“%@“,[hello lowercaseString]);//全部小写
NSLog(@“%@“,[hello capitalizeString]);//首字母大写,其他字母变小写
//转换为基本数据类型
NSString *st1 = @“10”;
int a = [str1 intValue];//将字符串转化为整形
NSString *str2 = @“10.25”;
float b = [str2 floatValue];//将字符串转化为字符型
NSString *str3 = @“1”;
BOOL flag = [str3 boolValue];//将字符串转化为泊尔类型
------------ 4.截取字符串——————————
分割字符串
NSString *str = @“aa,bb,cc,dd”;
NSArray *array = [str componentsSeparatedByString:@“,”];//使用“,”号将字符串分割开来,结果为aabbccdd
访问指定位置字符
NSString *string = @“abcdef”;
char s = [string characterAtIndex:1];//访问下标为1的字符,结果为b
截取到指定位置
NSString *string = @“abcdef”;
NSString *subString1 = [string substringToIndex:2];
//截取下标为2的前面的字符串,结果为ab
从指定位置截取到末尾
NSString *string = @“abcdef”;
NSString *subString2 = [string substringFromIndex:1];//截取从下标为1的位置到末尾,结果为bcdef
指定范围截取
NSString *string = @“abcdef”;
NSRange range = NSMakeRange(1,2);//NSRange表示范围(位置 长度)
NSString *subString3 = [string substringWithRange:range];//从下标为1截取到下标为2,结果为bc
------------5.拼接字符串——————————
拼接字符串
NSString *str1 = @“ab”;
NSString *str2 = @“cd”;
NSString *string1 = [str1 stringByAppendingString:str2];//结果abcd
------------6.查找 替换字符串——————————
查找字符串
NSString *substring = @“@qq.com”;
NSRang range = [email rangeOfString:substring];//判断email中是否包含substring字符串
if(range.location!=NSNotFound){
NSLog(@“%@包含%@“,email,substring);
NSLog(@位置是:%ld,长度是:%ld”,range.location,range.length);
}else{
NSLog(@“没找到");
}
替换字符串
NSString *newEmail = [email stringByReplacingOccurrencesOfString:@“qq.com”withString:@“163.com”];//判断email中是否含有qq.com 有的话 将qq.com改为163.com
可变字符串NSMutableString
NSMutableString是可变字符串 可以修改字符串中的内容
可变字符串继承与不可变字符串 不可变字符串中的所有方法,可变字符串都可以使用
------------.可变字符串的创建
——————————
NSMutableString *string1 = [NSMutableString stringWithString:@“abcd”];
NSMutableString *string2 = [[NSMutableString alloc] initWithString:@“asdsdsfdfh"];
插入字符串
NSMutableString *string = [NSMutableString stringWithString:@“sdf"];
[string insertString:@“a” atIndex:0];//在string的第0个位置插入a
删除字符串
NSMutableString *string = [NSMutableString stringWithString:@“aabb字符串"];
NSRange range = NSMakeRange(0,4);//NSMakeRange为范围 将下标范围0到4的字符给字符串range
[string deleteCharactersInRange:range];//删除range这个字符串
追加字符串
NSMutableString *string = [NSMutableString stringWithString:@“abc"];
[string appendString:@“def”];//将原来字符串加上def
替换字符串
NSMutableString *string = [[NSMutableString alloc] initWithString:@"kebian字符串"];
NSRange replaceRg = [string rangeOfString:@"kebian"];//先找到需要替换的字符串
if (replaceRg.location != NSNotFound) {
[string replaceCharactersInRange:replaceRg withString:@"可变"];
NSLog(@“string:%@“,string);//结果为 可变字符串
}