OC中字符串有兩種痕钢,一種是不可變字符串(NSString),一種是可變字符串(NSMutableString),不可變字符串中的內(nèi)容不允許被改變,而可變字符串中的內(nèi)容允許被改變仁卷。
不可變字符串NSString
首先創(chuàng)建一個空的字符串
NSString *initStr = [[NSString alloc]init];
NSString *initStr = [NSString string];
創(chuàng)建字符串的方法有下面幾種:
1.以字符串創(chuàng)建新的字符串
NSString *initStr = [[NSString alloc]initWithString:@"xiaoming"];
2.以字符串初始化字符串
NSString *initStr = [NSString stringWithString:@"xiaoming"];
3.按格式化創(chuàng)建新的字符串
NSString *initStr =[[NSString alloc]initWithFormat:@"a = %d",5];
NSString *initStr =[NSString stringWithFormat:@"%@",@"bajie"];
NSLog(@"initStr = %@",initStr);
NSLog(@"initStr = %@",initStr);
NSString一些常用的方法
1.將一個數(shù)字轉換成字符串
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)獲取第三個字符到倒數(shù)第二個字符
NSRange range2 = {2,initStr8.length-3};
NSString *subStr4 = [initStr8 substringWithRange:range];
NSLog(@"%@",subStr4);
5.字符串的比較(按照ascii碼表進行對比穴翩,對比規(guī)則同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)按格式化產(chǎn)生一個字符串并拼接到老字符串后面
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.替換(不可變字符串本身內(nèi)容不可以被改變,所以替換的結果是在被返回的新的字符串中)
NSString *str20 = @"I love you";? //首先定義一個字符串
(1)用字符串替換指定范圍內(nèi)的字符
NSRange range1 = {2,4};
NSString *replaceStr = [str20 stringByReplacingCharactersInRange:range withString:@"hate"];
NSLog(@"replaceStr = %@",replaceStr);
(2)用指定字符串替換指定所有出現(xiàn)的字符串
NSString *replaceStr1 = [str20stringByReplacingOccurrencesOfString:@"love"withString:@"hate"];
NSLog(@"replaceStr1 = %@",replaceStr1);
可變字符串NSMutableString
1.對象的創(chuàng)建方法
(1)創(chuàng)建一個新字符串哮幢,為字符串預留10個字符的空間
NSMutableString *mInitStr = [[NSMutableStringalloc]initWithCapacity:10];
(2)利用字符串來創(chuàng)建一個可變字符串(這是一個可變的字符串带膀,所以不可以用字面量)
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.將字符串轉換為數(shù)字
NSString *dStr = @"200";
NSInteger age = [dStr integerValue];
NSLog(@"%ld",age);
數(shù)值類
1.將數(shù)字18轉換成數(shù)值對象
NSNumber *number = [[NSNumber alloc]initWithInt:18];
2.將18.34轉換成數(shù)值對象
NSNumber *number1 = [[NSNumber alloc]initWithFloat:18.34];
3.將字符‘A’轉換成數(shù)值對象
NSNumber *number2 =[NSNumber numberWithChar:'A'];
4.將數(shù)值對象轉換成基本數(shù)據(jù)
NSInteger age1 = [number intValue];
NSLog(@"%ld",age1);
NSLog(@"%lf",number.floatValue);
NSLog(@"%c",number.charValue);
5.把BOOL變量轉換成數(shù)值
BOOL f = YES;
NSNumber *number4 = [NSNumber numberWithChar:f];
NSLog(@"%@",number4);
NSNumber *fnumber =@(f);
6.字面量 @(基本數(shù)據(jù)類型)
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);