OC中字符串和数值的方法与运用实例解析
OC中字符串有两种,一种是不可变字符串(NSString),一种是可变字符串(NSMutableString),不可变字符串中的内容不允许被改变,而可变字符串中的内容允许被改变。
不可变字符串NSString
首先创建一个空的字符串
NSString *initStr = [[NSString alloc]init];
NSString *initStr = [NSString string];
创建字符串的方法有下面几种:
1.以字符串创建新的字符串
NSString *initStr = [[NSString alloc]initWithString:@"xiaoming"];
2.以字符串初始化字符串
NSString *initStr = [NSString stringWithString:@"xiaoming"];
3.按格式化创建新的字符串
NSString *initStr =[[NSString alloc]initWithFormat:@"a = %d",5];
NSString *initStr =[NSString stringWithFormat:@"%@",@"bajie"];
NSLog(@"initStr = %@",initStr);
NSLog(@"initStr = %@",initStr);
NSString一些常用的方法
1.将一个数字转换成字符串
NSInteger a=100;
NSString *initStr7 = [[NSStringalloc]initWithFormat:@"ld",a];
NSLog(@"initStr7 = %@",initStr7);
2.字面量的表示(示例为获取字符串的长度)
NSString *initStr8 = @"dashixiong";
NSInteger len = initStr8.length;
3.获取指定的字符(characteAtIndex必须小于字符串的长度)
char c = [initStr8characterAtIndex:0];
NSLog(@"c = %c",c);
4.获取子串有三种方法
(1)从指定位置开始到结束
NSString *subStr = [initStr8 substringFromIndex:2];
NSLog(@"%@",subStr);
(2)从0开始到指定位置结束(不包含给定的位置)
NSString *subStr1 = [initStr8substringToIndex:2];
NSLog(@"subStr1 = %@",subStr1);
NSRange range = {1,2};
NSString *subStr3 = [initStr8 substringWithRange:range];
NSLog(@"%@",subStr3);
(3)获取第三个字符到倒数第二个字符
NSRange range2 = {2,initStr8.length-3};
NSString *subStr4 = [initStr8 substringWithRange:range];
NSLog(@"%@",subStr4);
5.字符串的比较(按照ascii码表进行对比,对比规则同strcmp一样)(字符串的比较是BOOL类型,所以输出的值有三种情况-1、0、1)
NSString *str1 =@"dashixiong";
NSString *str2 = @"ershixiong";
NSInteger result = [initStr8 compare:str2];
NSLog(@"result = %ld",result);
判断字符串是否相等:
BOOL flag = [str1isEqualToString:str2];
NSLog(@"%d",flag);
6.判断是否有前缀/后缀
flag = [str1 hasPrefix:@"er"]; //判断前缀
NSLog(@"flag = %d",flag);
flag = [str2 hasSuffix:@"xiong"]; //判断后缀
NSLog(@"flag = %d",flag);
7.拼接(拼接的结果在新的字符串中,所以这个方法是有返回值的,对拼接的两个字符串本身不做改变)
NSString *str5 = @"I love you"; //首先定义一个字符串
(1)将一个字符串拼接到老字符串后面
NSString *appendStr = [str5 stringByAppendingString:@"iPhone 5e"];
NSLog(@"%@",appendStr);
(2)按格式化产生一个字符串并拼接到老字符串后面
appendStr = [str5 stringByAppendingFormat:@"%d iPhone 5e",5];
NSLog(@"appendStr = %@",appendStr);
注意:字符串可以直接复制不需要方法
NSString *str10 =str5;
8.将字符串中所有字符大/小写、单词首字母大写
(1)全部大写
NSString *upStr = [str5uppercaseString];
NSLog(@"upStr = %@",upStr);
(2)全部小写
NSString *lowStr = [str5lowercaseString];
NSLog(@"lowStr =%@",lowStr);
(3)单词首字母大写
NSString *capStr = [str5capitalizedString];
NSLog(@"CapStr = %@",capStr);
9.替换(不可变字符串本身内容不可以被改变,所以替换的结果是在被返回的新的字符串中)
NSString *str20 = @"I love you"; //首先定义一个字符串
(1)用字符串替换指定范围内的字符
NSRange range1 = {2,4};
NSString *replaceStr = [str20 stringByReplacingCharactersInRange:range withString:@"hate"];
NSLog(@"replaceStr = %@",replaceStr);
(2)用指定字符串替换指定所有出现的字符串
NSString *replaceStr1 = [str20stringByReplacingOccurrencesOfString:@"love"withString:@"hate"];
NSLog(@"replaceStr1 = %@",replaceStr1);
可变字符串NSMutableString
1.对象的创建方法
(1)创建一个新字符串,为字符串预留10个字符的空间
NSMutableString *mInitStr = [[NSMutableStringalloc]initWithCapacity:10];
(2)利用字符串来创建一个可变字符串(这是一个可变的字符串,所以不可以用字面量)
NSMutableString *mInitStr2 = [[NSMutableString alloc]initWithString:@"shifu"];
2.拼接
[mInitStr2 appendString:@"大师兄不见了"];
NSLog(@"%@",mInitStr2);
[mInitStr2 appendFormat:@"I have %d",8];
NSLog(@"%@",mInitStr2);
3.插入 (在指定下标处插入字符串)
[mInitStr2 insertString:@"ershixiong" atIndex:6];
NSLog(@"%@",mInitStr2);
4.删除
NSMutableString *mStr =[NSMutableStringstringWithString:@"I love you"];
NSRange range5 ={0,1};
[mStrdeleteCharactersInRange:range5];
NSLog(@"%@",mStr);
5.替换(replaceOccurrencesOfString中输入的是被替换的字符串,
withString中输入的是新字符串,options中输入的是查找的方式,range中写的是范围)
注意:options是枚举类型
NSRange range6 = {2,7};
[mStrreplaceOccurrencesOfString:@"o"withString:@"ab"options:NSCaseInsensitiveSearchrange:range6];
NSLog(@"%@",mStr);
NSString *reStr = [mStrstringByReplacingOccurrencesOfString:@"ab"withString:@"o"];
NSLog(@"%@",reStr);
6.将字符串转换为数字
NSString *dStr = @"200";
NSInteger age = [dStr integerValue];
NSLog(@"%ld",age);
数值类
1.将数字18转换成数值对象
NSNumber *number = [[NSNumber alloc]initWithInt:18];
2.将18.34转换成数值对象
NSNumber *number1 = [[NSNumber alloc]initWithFloat:18.34];
3.将字符‘A’转换成数值对象
NSNumber *number2 =[NSNumber numberWithChar:'A'];
4.将数值对象转换成基本数据
NSInteger age1 = [number intValue];
NSLog(@"%ld",age1);
NSLog(@"%lf",number.floatValue);
NSLog(@"%c",number.charValue);
5.把BOOL变量转换成数值
BOOL f = YES;
NSNumber *number4 = [NSNumber numberWithChar:f];
NSLog(@"%@",number4);
NSNumber *fnumber =@(f);
6.字面量 @(基本数据类型)
NSNumber *zNumber =@('A');
NSValue
1.将点结构体转换成对象
NSPoint point = {1,2};
NSValue *value = [NSValuevalueWithPoint:point];
2.将对象转换成结构体
NSPoint pp = value.pointValue;
NSLog(@"pp.x = %.f , pp.y =%.f",pp.x,pp.y);
3.将自定义结构体转换成对象
typedef struct student //首先在import下面main上方写一个结构体
{
char name[20];
char sex;
int age;
float score;
int num;
}Student;
//回到main里面
struct student stu ={"xiaohong",'F',19,100,11};
NSValue *stuValue =[[NSValue alloc]initWithBytes:&stu objCType:@encode(struct student)];
4.将对象转换成自定义的结构体(刚刚的student为例)
struct student stu1;
[stuValue getValue:&stu1];
NSLog(@"name = %s,sex =%c,age=%d,score =%f,num =%d",stu1.name,stu1.sex,stu1.age,stu1.score,stu1.num);