字符串的创建
第一种方式:
char a[] = "lanOu";
NSString *string1= [[NSString alloc] initWithUTF8String:a];//将C语言中字符串转换为OC中的字符串。
NSLog(@"%@",string1);
NSString *string2 = [[NSString stringWithUTf8String:a];
NSLog(@"%@",string2);////便利构造器方法
第二种方式:字面量(最常用的字符串创建方式)
NSString *string3 = @“lanOu3g”;
NSLog(@“%@”,string3);
第三种方式(最NB的创建方式)
NSString *str = [NSString alloc] initWithFormat"yangyu"];
二:字符串长度
NSString *string = [NSString stringWithFormat:@“lanou”];
//NSUInterger无符号长整型
NSUInterger length = string.length;
NSLog(@“%lu”,length);
获取字符串的字符
unichar字符中的字符类型无符号短整型。
characterAtInder:获取字符串下标 从0开始
NSString *string = [NSString stringWithFormat:@"lnaOu"];
uinchar c= [string characterAtInder:2];
NSLog(@"%c",c);结果是:Y
/******判断字符串是否相等*****/
isEqualToSring 判断字符串是否相等 (判断两个字符串的地址是否相等)
NSString *string1 = @"heihei";
NSString *string2 = @"lala";
if([string1 isEqualTostring:string2]){
NSLog(@"两个字符串相等");
}else{
NSLog(@"两个字符串不想等");
}
//下面的判断是字符串的地址
if(string1 == string2){
NSLog(@"地址相等");
}else{
NSLog(@"地址不想等");
}
/*****字符串比较*****/
NSComparisonResult result
NSString *string1 = @"laoYang";
NSString *string2 = @"yaLi";
NSComparisonResult result = [string1 compare:srting2];
switch(result){
case NSOrederedAscending:
NSL(@"前大后小");
break;
case NSOrederedDescending:
NSLog(@"前大后小");
break;
default(@"相等");
break;
}
/*****获取字符串*****/
(1)substringToIndex:获取下标为x之前的字符串
NSString *string = @"lanOu";
NSString *string1 = [string substringToIndex:5];
NSLog(@"%@",string1);
(2)substringFromIndex:获取下标为x之后的字符,包含下标为x的字符
NSString *string2 = [string substringFromIndex:5];
NSLog(@"%@",string2);
(3)substringWithRange:NSMakeRage(x,y):截取下标X开始之后Y位数
NSString *string3 = [string substringWithRange:NSMakeRage(3,4)];
NSLog(@"%@",string3);
/*****字符串拼接*****/
NSString *string = @"大连";
NSString *string2 = [string stringByAppendString:@"蓝欧"];
NSLog(@"%@",string2);
/***********字符串替换*******/
//前面的a是我想要替换的字符串,后面的b是我想要替换成的字符串。
stringByRepalcingOccurrencesOfString:@"a"withString:@"b"];
//从下标x开始后面 Y个字符串替换成”字符串“
stringByRepalcingCharactersInRange:NSMakkeRange(x, y)withString:@"heihei"];
/********字符串转换成其他类型*******/
NSString *string = @"123";
//将字符串转换成长整型
NSIntegeger a = [string integerVslue];
NSLog(@"%ld",a);
NSString *string1 = @"19.8";
float b = [string1 foatValue];
NSLog(@"%f",b);
NSString *string2 = @"18";
int c = [string2 intValue];
NSLog(@"/d",c);
/**********************大小写转换*********************、
NSString *string = @"hello lanOu";
(1)全部大写
NSString *upperString = [string uppercaseString];
NSLog(@"%@",upperString);
(2)全部小写
NSString *lowwerStr = [string lowercaseString];
NSLog(@"%@",lowerStr);
(3)首字母大写
NSString *capitalStr = [string capitalizedString];
NSLog(@"%@",capitalStr);
/************判断开头结尾是否有指定字符**************/
NSString *string = @"http://www.lanou3g.com";
判断是否以http开头
BOOL isPrefix = [string hasPrefix:@"http"];
NSLog(@"%d",isPrefix);
判断是否以com结尾
BOOL isSuffix = [string hasSuffix:@"com"];
NSLog(@"%d",isSuffif);
可变字符串
NSMutableString *mStr = [[NSMutableString alloc] initWithFormat:@"dalian"];
NSLog(@"%@",mStr);
在dalian之前拼接lanou
[mStr appendFormat:@"lanOu%ld",1];
NSLog(@“%@”,mStr);
[mStr appendString:@"heihei"];
NSLog(@"%@",mStr);
/****插入字符串****/
[mStr insertString:@"haha" atIndex:0];//在下标为0前面插入字符串
/*****删除字符串******/
[mStr deleteCharacteersInRangre:NSMakeRange(3,2)];//从第三个开始往后数两个(包括第三个)
/******替换字符串******/
【mStr repelaceCharateraInRange:NSMakeRange(3,2)WithString:@"123"];
/*******重置字符串*****/
[mStr setString:@"heihei"];