关于可变字符串基础知识
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableString *mustr1=[[NSMutableString alloc]init];
NSMutableString *mustr2=[[NSMutableString alloc]initWithFormat:@"Hello"];
NSMutableString *mustr3=[[NSMutableString alloc]init];
//赋值
NSString *str1=@"Welcom to OC";
mustr1=[[NSMutableString alloc]initWithString:str1];
//NSMutableString *mustr3=@"Hello"; 错误的赋值法,不能将字符串常量直接赋值给NSMutableString
//替换字符串
NSRange rang1=[mustr1 rangeOfString:@"OC"];
[mustr1 replaceCharactersInRange:rang1 withString:@"Objective-C"];
NSLog(@"mustr1=%@",mustr1);
//插入字符串
[mustr1 insertString:@" students" atIndex:6]; //在指定位置插入字符串
[mustr2 appendString:@" teacher!"]; //在末尾插入字符串
[mustr3 appendFormat:@"Hey!%@",str1]; //格式化插入字符串
NSLog(@"mustr1=%@,mustr2=%@,mustr3=%@",mustr1,mustr2,mustr3);
//指定位置删除字符串
[mustr1 deleteCharactersInRange:NSMakeRange(7, 9)];
NSLog(@"mustr1=%@",mustr1);
//查找删除字符串
NSRange rang2=[mustr2 rangeOfString:@" teacher"];
if(rang2.location!=NSNotFound){
[mustr2 deleteCharactersInRange:rang2];
}
NSLog(@"%@",mustr2);
//重新赋值
[mustr1 setString:str1];
NSLog(@"%@",mustr1);
}
int a=345,b=789,c=123,d=567;
NSLog(@"a=%d,b=%d,c=%d,d=%d",a,b,c,d);
NSNumber *numA=[[NSNumber alloc]initWithInt:a];
NSNumber *numB=[[NSNumber alloc]initWithInt:b];
NSNumber *numC=[[NSNumber alloc]initWithInt:c];
NSNumber *numD=[[NSNumber alloc]initWithInt:d];
NSArray *array1=[NSArray arrayWithObjects:numA,numB,numC,numD,nil];
NSArray *array2=[array1 sortedArrayUsingSelector:@selector(compare:)];
for(NSArray *array3 in array2){
NSLog(@"%@",array3);
}
//判断字符串前缀
NSString *str1=@"iOS-study";
BOOL result1=[str1 hasPrefix:@"iOS"];
//判断字符串后缀
NSString *str2=@"study.jpg";
BOOL result2=[str2 hasSuffix:@".jpg"];
//分割字符串
NSString *str3=@"i am a new programmer";
NSArray *array=[str3 componentsSeparatedByString:@" "]; //根据给定字符分割
NSLog(@"%@",array); //直接打印数组(一般不允许用在正式程序中)
//截取字符串
NSString *str4=[str3 substringToIndex:4]; //从字符串开始截取到第4位之前
NSString *str5=[str3 substringFromIndex:4]; //从第4位开始截取到字符串末位
//根据位置和截取长度截取字符串
NSRange rang1;
rang1.location=7;
rang1.length=3;
NSString *str6=[str3 substringWithRange:rang1]; //根据rang截取第7位开始的3个字符
NSRange rang2=[str3 rangeOfString:@"programmer"]; //获得字符串的起始位置和长度
//拼接字符串
NSString *str7=@"Good",*str8=@"bye";
NSString *str9=[[NSString alloc]initWithFormat:@"%@%@",str7,str8]; //实例初始化法拼接
NSString *str10=[str7 stringByAppendingString:str8]; //直接拼接
NSString *str11=[str7 stringByAppendingFormat:@"-%@",str8]; //拼接中加入其他字符
//查询字符串
NSRange rang3=[str3 rangeOfString:@"new"]; //查询字符串的位置
NSLog(@"%@",NSStringFromRange(rang3)); //显示字符串所在位置
if(rang3.location==NSNotFound){
NSLog(@"Can't find");
}
else{
NSLog(@"location=%ld,length=%ld",rang3.location,rang3.length);
}
}
//创建字符串常量
NSString *str1=@"char1";
//实例方法创建
NSString *str2=[[NSString alloc]init];
NSString *str4=[[NSString alloc]initWithFormat:@"char4"];
NSString *str5=[[NSString alloc]initWithString:str1];
//类方法创建
NSString *str6=[NSString string];
NSString *str7=[NSString stringWithString:str1];
NSString *str8=[NSString stringWithFormat:@"char8"];
//格式化创建
int num1=3;
float num2=4.5;
NSString *str9=[[NSString alloc]initWithFormat:@"%f",num2];
NSString *str10=[[NSString alloc]initWithFormat:@"%d,%f",num1,num2];
NSString *str11=[NSString stringWithFormat:@"%d,%f",num1,num2];
//字符串长度
NSUInteger len=[str1 length];
//字符串比较
if([str7 isEqualToString:str8]){
NSLog(@"They are sam");
}
else{
NSLog(@"They are different");
}
//判断字符串大小
NSComparisonResult result1=[str7 compare:str8];
NSComparisonResult result2=[str7 caseInsensitiveCompare:str8];
//字符串转换
float num3=[str9 floatValue];
//大小写转换
NSString *str12=[[NSString alloc]initWithFormat:@"HelloWorld!"];
NSString *str13=[str12 uppercaseString];
NSString *str14=[str12 lowercaseString];
NSString *str15=[str12 capitalizedString];
//将NSString转换为C语言字符串
const char * str16=[str12 UTF8String];
return 0;
}