OC中字符串分为可变字符串和不可变字符串,灵活性要强于C语言;下面介绍一下OC中不可变字符串的常用功能;
#import
/*
OC的字符串和c的字符串的差别:
c的组成是char,以ASCII存储,以'\0'结束
OC:字符串的对象;基本组成是unichar
OC:不可变字符串NSString ;可变字符串NSMutableString
*/
//int main(int argc, const char * argv[])
//{
// @autoreleasepool
// {
char str[100]="helloworld";//字符串数组
char *p = "helloworld";//常量字符串
// NSString *str2 = @"你好helloworrld";//
// unichar ch = [str2 characterAtIndex:0];
// NSLog(@"%C",ch);//可以打印OC的字符,可以打印中文字符;characterAtIndex
// }
// return 0;
//}
#if 0
int main()
{
@autoreleasepool
{
//1.新建一个字符串,(对象方法,类方法)
//把C串改成OC串;
NSString *str1 = [[NSString alloc]initWithUTF8String:"HELLOworld"];
NSLog(@"str1=%@",str1);//%@ 表示打印对象;
//格式化字符串
NSString *str2 = [[NSString alloc]initWithFormat:@"HELLO WORLD %d",1581];
NSLog(@"str2=%@",str2);
//根据其他字符串,创建一个字符串
NSString *str3 = [[NSString alloc]initWithString:str2];
NSLog(@"str3=%@",str3);
//表示类方法;
NSString *str4 = [NSString stringWithUTF8String:"hello"];
NSLog(@"str4=%@",str4);
NSString *str5 = [NSString stringWithFormat:@"你好%d",2015];
NSLog(@"str5=%@",str5);
NSString *str6 = [NSString stringWithString:str5];
NSLog(@"str6=%@",str6);
NSString *str7 = @"你好hello";//直接用常量字符串对象赋值;
NSLog(@"str7=%@",str7);
//2.字符串的长度:OC字符串的个数,中文字符算一个;
NSInteger len = [str7 length];
//NSInteger-->long 64位系统下8字节
NSLog(@"len=%ld",len);//len=7
//3.提取字符
unichar ch = [str7 characterAtIndex:1];
NSLog(@"%C",ch);//好 大写c打印的是unichar
//4.判断字符串是否相等
NSString *str8 = @"helloa";
NSString *str9 = @"helloA";
BOOL cmp = [str8 isEqualToString:str9];
if(cmp == YES)
{
NSLog(@"字符串相等");
}
else
{
NSLog(@"字符串不相等");
}
//5.字符串大小的比较 NSComparisonResult是个枚举类型
NSComparisonResult cmprst= [str8 compare:str9];
if(cmprst == NSOrderedSame)
{
NSLog(@"str8和str9相等");
}
else if(cmprst == NSOrderedAscending)
{
NSLog(@"str8比str9小");
}
else
{
NSLog(@"str8比str9大");
}
//case:大小写lower(小写) upper(大写) Insensitive(麻木不区分大小写)
//caseInsensitiveCompare 忽略大小写比较
NSComparisonResult cmprst2 = [str8 caseInsensitiveCompare:str9];
if(cmprst2 == NSOrderedSame)
{
NSLog(@"str8和str9相等");
}
else if(cmprst2 == NSOrderedAscending)
{
NSLog(@"str8比str9小");
}
else
{
NSLog(@"str8比str9大");
}
//6.字符串查找
NSString *str10 = @"qianfeng is a good trainning school";
NSString *search1 = @"qianfeng";
//在str10中查找search这个字符串的位置
NSRange range = [str10 rangeOfString:search1];
//location 表示字符串出现的位置 length 表示长度
NSLog(@"qianfeng出现的位置是%lu,长度是%lu",range.location,range.length);//qianfeng出现的位置是0,长度是8
NSString *search2 = @"QF";
range = [str10 rangeOfString:search2];
//NSNotfoud = max_long 非常大的一个数
if(range.location == NSNotFound)
{
NSLog(@"没有找到");
}
else
{
NSLog(@"qianfeng出现的位置是%lu,长度是%lu",range.location,range.length);//qianfeng出现的位置是0,长度是8
}
}
return 0;
}
#endif
#if 0
134//不会编译.
#endif
#if 0
//字符串的常用操作
int main()
{
@autoreleasepool
{
//1.判断前后缀
NSString *str1 = @"www.baidu.com";
if([str1 hasPrefix:@"www."] == YES)
{
NSLog(@"有前缀www.");
}
else
{
NSLog(@"没有前缀www.");
}
if([str1 hasSuffix:@".com"] == YES)
{
NSLog(@"有后缀.com");
}
else
{
NSLog(@"没有后缀.com");
}
//2.字符串转化数字.字符串类型转成基本数据类型
NSString *str2 = @"1234.99";
NSInteger nsi = [str2 integerValue];
NSLog(@"str2->integer is:%ld",nsi);
double d = [str2 doubleValue];
NSLog(@"str2->double is:%lf",d);
long long ll = [str2 longLongValue];
NSLog(@"str2->longlong is:%lld",ll);
NSString *str3 = @"True";//第一个字符:"Y","y","t","T",or a digit 1~9 返回1;
BOOL b = [str3 boolValue];
NSLog(@"str2->BOOL is:%d",b);//str2->BOOL is:1
//3.大小写转化
NSString *str4 = @"abc";
NSString *str5 = [str4 uppercaseString];//创建了新的字符串;有[[alloc]init]过程
NSLog(@"str5=%@,str4=%@",str5,str4);//str5=ABC,str4=abc
str4 = @"hello";//str4是NSString常量,指向不可变字符串,指向的内存空间不可以改变,但是指向可以改;就相当于C语言党只能给的 const *p,*p不可以改变,但是p可以改;
NSLog(@"str4=%@",str4);//str4=hello
NSString *str6 = [str5 lowercaseString];
NSLog(@"str6=%@,str5=%@",str6,str5);//str6=abc,str5=ABC
NSString *str7 = [str6 capitalizedString];
NSLog(@"str7=%@,str6=%@",str7,str6);//str7=Abc,str6=abc
//4.子串的提取
NSString *str8 = @"起来不愿做奴隶的人们";
NSString *str9 = [str8 substringFromIndex:1];
NSLog(@"str9=%@",str9);//str9=来不愿做奴隶的人们
str9 = [str8 substringToIndex:3];
NSLog(@"str9=%@",str9);//str9=起来不 一共取三个;
NSRange range = {0,5};//第一个参数表示起始位置,第二个参数表示长度;
str9 = [str8 substringWithRange:range];
NSLog(@"str9=%@",str9);
// //5.从本地文件读取文件
// NSString *path = @"/Users/qianfeng/Desktop/Objective-c/上课笔记/第2天/1.NSString/text";
// NSString *str10 = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
// NSLog(@"str10=%@",str10);
//
// NSError *err;//不能创建对象分配空间
// str10 = [[NSString alloc]initWithContentsOfFile:path usedEncoding:NSUTF8StringEncoding error:&err];
// NSLog(@"str10=%@",str10);
// if(err)//err!=nil表示出错,err表示实在调用的时候创建
// {
// NSLog(@"读文件出错,错误信息是%@",err);
// }
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSString *str10 = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str10);
str10 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str10);
}
return 0;
}
#endif
int main()
{
@autoreleasepool {
//1.分割字符串
NSString *str = @"hello &i an &happy";
//NSArray OC数组,里面放对象
NSString *demi = @" &";//只有str里面完整的出现 " &"才会分割;
NSArray *array = [str componentsSeparatedByString:demi];
//分离后 components(零件) 就是字符串对象
//array 里面就是字符串对象
NSLog(@"%@",array);
//set集合 是无序的 里面有3个元素 : & ,空格,#
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"& #"];
NSArray *array2=[str componentsSeparatedByCharactersInSet:set];
//在分割的时候就认为在每个字符的2边都存在内容,如果没有就分割出空字符
NSLog(@"%@",array2);
NSString *str2 = @"zfz,F,123,FISH,,,";
NSString *str3 = @"zfz,F,456,CAT,benz,lch,1000";
NSCharacterSet *set2 = [NSCharacterSet characterSetWithCharactersInString:@","];
NSArray *array3 = [str2 componentsSeparatedByCharactersInSet:set2];
NSLog(@"array3有%ld个元素,数组如下%@",[array3 count],array3);
NSArray *array4 = [str3 componentsSeparatedByCharactersInSet:set2];
NSLog(@"array4有%ld个元素,数组如下%@",[array4 count],array4);
NSString *str4 = [array4 componentsJoinedByString:@" "];
NSLog(@"%@",str4);
//字符串追加
NSString *str5 = @"hello";//@"123"
NSString *str6 = [str5 stringByAppendingString:@"123"];
NSLog(@"str5=%@,str6=%@",str5,str6);//在str5的基础上构造str6(在另外的内存空间),str5本身没有变,
NSLog(@"str6=%p",str6);
str6 = [str5 stringByAppendingFormat:@"%d",1519];//str6=hello1519
NSLog(@"str6=%@",str6);//新构造的字符串,指针改变指向;
NSLog(@"str6=%p",str6);//str6是不可改变字符串,但是指向可以变,这是一个新建字符串,指针指向新的字符串;
//替换:str6本身没有改变只是利用str6的样子去新建了一个字符串;
NSString *str7 = [str6 stringByReplacingOccurrencesOfString:@"hello" withString:@"你好"];
NSLog(@"str6=%@,str7=%@",str6,str7);//str6=hello1519,str7=你好1519
//替换的是(0~N)范围内的字符;
NSString *str8 = [str6 stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@"超级土豪"];
NSLog(@"str8=%@",str8);
}
}