1.创建常量字符串
//通过这种方式创建的字符串只能是不可变的
NSString*str1 =@"不可变的OC字符串";
/*****通过对象方法(实例方法)创建********/
//2.通过str1去创建str2
NSString*str2 = [[NSStringalloc]initWithString:str1];
NSLog(@"str1 = %@,str2 = %@",str1,str2);
//3.把C字符串转换成OC字符串
NSString*str3 = [[NSStringalloc]initWithUTF8String:"WHiOS1603"];
NSLog(@"str3 = %@",str3);
//4.**重点掌握**
//通过格式符去创建(拼接)字符串
NSString*str4 = [[NSStringalloc]initWithFormat:@"%d-%.2f-%c-%s",100,3.1415926,'S',"SY"];
NSLog(@"str4 = %@",str4);
/******通过类方法创建字符串对象******/
NSString*str5 = [NSStringstringWithString:str1];
NSString*str6 = [NSStringstringWithUTF8String:"Hello OC"];
NSString*str7 = [NSStringstringWithFormat:@"拼接字符串:%.2f--%c--%d",3.1415,'A',10];
NSLog(@"str5 = %@",str5);
NSLog(@"str6 = %@",str6);
NSLog(@"str7 = %@",str7);
/*****把OC字符串对象转换成基础数据类型*****/
NSString*str =@"3.1415926";
//1.转换成int类型
int a = str.intValue;
//2.NSInteger 32->int 64->long
NSIntegerinteger = str.integerValue;
NSLog(@"a = %d",a);
NSLog(@"integer = %ld",integer);
//3.转换成BOOL类型YES 1,NO 0
BOOLbl = str.boolValue;
NSLog(@"bl = %d",bl);
//4.把OC字符串转换成C字符串
constchar*s = [strUTF8String];
NSLog(@"s = %s",s);
/**返回字符串的长度**/
//NSUInteger 32->无符号整型64->无符号长整型
NSUIntegerlen =[strlength];//OC字符串没有尾零
NSLog(@"len = %ld",len);
//取出字符串中的单个字符,unichar就代表c中的char
unicharc = [strcharacterAtIndex:5];
NSLog(@"c = %c",c);
NSString*str1 =@"abcd2017ZXCV";
NSString*subStr1 =@"2017";
1.字符串的查找(查找子串在父串中的位置和范围)NSRang
//NSRange是一个结构体,包含了location和length两部分
//location子串在父串中的起始位置length子串的长度
NSRangerange = [str1rangeOfString:subStr1];
NSLog(@"location:%ld length:%ld",range.location,range.length);
//通过length的值来判断是否查找成功
if( range.length>0) {
NSLog(@"找到了");
}else{
NSLog(@"父串中没有该字符串");
}
2.字符串的提取
//从某个位置开始一直截取到字符串结尾
NSString*subStr2 = [str1substringFromIndex:8];
NSLog(@"subStr2 = %@",subStr2);
//从开始位置截取,截取到某个位置(注:但不包含截取到的这个位置)
NSString*subStr3 =[str1substringToIndex:4];
NSLog(@"subStr3 = %@",subStr3);
//NSRange rg = {4,6};
NSRangerg =NSMakeRange(4,6);
NSString*subStr4 =[str1substringWithRange:rg];
NSLog(@"subStr4 = %@",subStr4);
/*****字符串的比较*****/
NSString*str1 =@"SSY2017OBJC";
NSString*str2 =@"sSY2017OBJC";
//1.判断两个字符串是否相等
BOOLbl = [str1isEqualToString:str2];
if(bl ==YES) {
NSLog(@"相等!");
}else{
NSLog(@"不相等");
}
//2.比较两个字符串的大小
NSComparisonResultresult =[str1compare:str2];
/*
NSOrderedAscending递增
NSOrderedSame相等
NSOrderedDescending递减
*/
if(result ==NSOrderedAscending) {
NSLog(@"str1小于str2");
}elseif(result ==NSOrderedSame){
NSLog(@"str1等于str2");
}else{
NSLog(@"str1大于str2");
}
//3.不区分大小写比较两个字符串的大小
NSComparisonResultresult2 =[str1caseInsensitiveCompare:str2];
if(result2 ==NSOrderedAscending) {
NSLog(@"str1
}elseif(result2 ==NSOrderedSame){
NSLog(@"str1=str2");
}else{
NSLog(@"str1>str2");
}
/**字符串大小写转换**/
NSString*string =@"abcdxyz";
//转换成大写
NSString*uppStr = [stringuppercaseString];
NSLog(@"uppStr = %@",uppStr);
//大写转小写
NSString*lowStr =[uppStrlowercaseString];
NSLog(@"lowStr = %@",lowStr);
//首字母大写,其余全部小写
NSString*capStr =[stringcapitalizedString];
NSLog(@"capStr = %@",capStr);
/**判断字符串的前后缀**/
NSString*string1 =@"shisiheng2017.txt";
NSString*preStr =@"shi";
NSString*suffStr =@".txt";
//1.判断前缀
BOOLrect1 = [string1hasPrefix:preStr];
if(rect1) {
NSLog(@"是前缀");
}else{
NSLog(@"不是前缀");
}
//2判断后缀
BOOLrect2 = [string1hasSuffix:suffStr];
if(rect2) {
NSLog(@"是后缀");
}else{
NSLog(@"不是后缀");
}
/**字符串的各种拼接**/
//1.在原来的字符串的基础上,接着往后拼接,返回拼接之后的新字符串
NSString*string2 =@"Hello,Girl!";
NSString*newStr1 = [string2stringByAppendingString:@" I Love You!!!"];
NSLog(@"newStr1 = %@",newStr1);
//2.格式符拼接
NSString*newStr2 = [string2stringByAppendingFormat:@"%d-%@",999,@"~玫瑰~"];
NSLog(@"newStr2 = %@",newStr2);
//3.拼接路径(默认加上/)
NSString*newStr3 = [string2stringByAppendingPathComponent:@"love.ppt"];
NSLog(@"newStr3 = %@",newStr3);
//1创建一个空的字符串@""
NSMutableString*mStr1 = [[NSMutableStringalloc]init];
NSMutableString*mStr2 = [NSMutableStringstring];
//2.创建一个预设大小的可变字符串
NSMutableString*mStr3 = [[NSMutableStringalloc]initWithCapacity:20];
NSMutableString*mStr4 = [NSMutableStringstringWithCapacity:10];
//3.通过不可变字符串创建出一个可变字符串(把不可变字符串转换成可变字符串)
NSString*str =@"我是字符串";
NSMutableString*mStr5 = [[NSMutableStringalloc]initWithString:str];
NSMutableString*mStr6 = [NSMutableStringstringWithString:str];
NSLog(@"mStr1 = %@",mStr1);
NSLog(@"mStr2 = %@",mStr2);
NSLog(@"mStr3 = %@",mStr3);
NSLog(@"mStr4 = %@",mStr4);
NSLog(@"mStr5 = %@",mStr5);
NSLog(@"mStr6 = %@",mStr6);
//**增**//
//1.在指定的位置插入新的字符串
[mStr6insertString:@"我们"atIndex:2];
NSLog(@"mStr6 = %@",mStr6);
//2.在原来的字符串的基础上追加一个字符串
[mStr6appendString:@"-可变字符串"];
NSLog(@"mStr6 = %@",mStr6);
//3.在原来的字符串的基础上格式符追加
[mStr6appendFormat:@"%s","NSMutableString"];
NSLog(@"mStr6 = %@",mStr6);
//**删除**//
//删除指定范围的字符串
NSRangerange = {6,3};
[mStr6deleteCharactersInRange:range];
NSLog(@"mStr6 = %@",mStr6);
//**修改**//
//1替换掉指定范围的字符串
NSRangerange1 = {0,2};
[mStr6replaceCharactersInRange:range1withString:@"现在"];
NSLog(@"mStr6 = %@",mStr6);
//2.直接用一个新字符串全部替换掉原来的字符串
NSString*newStr =@"这都是什么鬼!";
[mStr6setString:newStr];
NSLog(@"mStr6 = %@",mStr6);