NSSrting的几种常用的用法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u012701023/article/details/47125027
1.创建NSString字符串

NSString 与 char* 最大的区别就是 NSString是一个objective对象,而char* 是一个字节数组。@+" 字符串 " 这个符号为objective-c NSString 字符串常量的标准用法,char* 创建的时候 无需添加@

- (void)viewDidLoad  
    [super viewDidLoad];  
    //经典的字符串赋值  
    NSString *str0 = @"my name is justcoding !";  
    //字符串格式化合并分别包括  
    //NSString*类型 int类型 char*类型   
    NSString *str1 = [NSString stringWithFormat:@"我的名字:%@ 我的年龄:%d 我的邮箱:%s",@"justcoding", 25,"justcoding@gmail.com"];  
    //字符串赋值 参数中只可以写一个字符串 和第一种很像  
    NSString *str2 = [NSString stringWithString:@"我是字符串"];  
    //字符串转换为utf-8格式 参数为char*类型  
    NSString *str3 = [NSString stringWithUTF8String:"字符串转换utf-8格式"];  
    //字符串合并  
    int i  = 100;  
    char*c = "xuanyusong";  
    NSString *temp = @"我是临时字符串";  
    //在字符串temp的基础继续添加 int i 与 char* c 组成一个新的字符串  
    NSString *str4 = [temp stringByAppendingFormat:@"整型: %d 字符型 :%s",i,c];  
    //在字符串temp的基础继续添加temp 并组成一个新的字符串  
    NSString *str5 = [temp stringByAppendingString:temp];   
    //字符串输出  
    NSLog(@"str0 = %@", str0);  
    NSLog(@"str1 = %@", str1);  
    NSLog(@"str2 = %@", str2);  
    NSLog(@"str3 = %@", str3);  
    NSLog(@"str4 = %@", str4);  
    NSLog(@"str5 = %@", str5);  
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //经典的字符串赋值
    NSString *str0 = @"my name is justcoding !";
    
    //字符串格式化合并分别包括
    //NSString*类型 int类型 char*类型 
    NSString *str1 = [NSString stringWithFormat:@"我的名字:%@ 我的年龄:%d 我的邮箱:%s",@"justcoding", 25,"justcoding@gmail.com"];
    
    //字符串赋值 参数中只可以写一个字符串 和第一种很像
    NSString *str2 = [NSString stringWithString:@"我是字符串"];
   
    //字符串转换为utf-8格式 参数为char*类型
    NSString *str3 = [NSString stringWithUTF8String:"字符串转换utf-8格式"];
    
    
    //字符串合并
    int i  = 100;
    char*c = "xuanyusong";
    NSString *temp = @"我是临时字符串";
    
    //在字符串temp的基础继续添加 int i 与 char* c 组成一个新的字符串
    NSString *str4 = [temp stringByAppendingFormat:@"整型: %d 字符型 :%s",i,c];
    
    //在字符串temp的基础继续添加temp 并组成一个新的字符串
    NSString *str5 = [temp stringByAppendingString:temp]; 
    
    //字符串输出
    NSLog(@"str0 = %@", str0);
    NSLog(@"str1 = %@", str1);
    NSLog(@"str2 = %@", str2);
    NSLog(@"str3 = %@", str3);
    NSLog(@"str4 = %@", str4);
    NSLog(@"str5 = %@", str5);
    
}
2.字符串的遍历 

每一个字符串其实是由若干个char字符组成,字符串的遍历实际上就是将字符串中的每一个字符提取出来。

- (void)viewDidLoad  
    [super viewDidLoad];  
    //经典的字符串赋值  
    NSString *str = @"YUSONGMOMO";  
    //字符串的长度  
    int count = [str length];  
    NSLog(@"字符串的长度是%d",count);  
    //遍历字符串中的每一个字符  
    for(int i =0; i < count; i++)  
        char c = [str characterAtIndex:i];  
        NSLog(@"字符串第 %d 位为 %c",i,c);  
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //经典的字符串赋值
    NSString *str = @"YUSONGMOMO";
    
    //字符串的长度
    int count = [str length];
    
    NSLog(@"字符串的长度是%d",count);
    
    //遍历字符串中的每一个字符
    for(int i =0; i < count; i++)
    {
        char c = [str characterAtIndex:i];
        NSLog(@"字符串第 %d 位为 %c",i,c);
    }
    
}
 

3.字符串的比较 

isEqualToString 比较字符串是否完全相等,大小写不一样也无法完全匹配。

hasPrefixe 匹配字符串头

haSuffix     匹配字符串的尾巴

- (void)viewDidLoad  
    [super viewDidLoad];  
    NSString *str0 = @"justcoding";  
    NSString *str1 = @"justcoding";  
    //字符串完全相等比较  
    if([str0 isEqualToString:str1])  
        NSLog(@"字符串完全相等");  
    //字符串以开头比较  
    if([str0 hasPrefix:@"just"])  
        NSLog(@"字符串str0以just开头");  
    //字符串以结尾比较  
    if([str1 hasSuffix:@"coding"])  
        NSLog(@"str1字符串以coding结尾");  
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    NSString *str0 = @"justcoding";
    NSString *str1 = @"justcoding";
    
    
    //字符串完全相等比较
    if([str0 isEqualToString:str1])
    {
        NSLog(@"字符串完全相等");
    }
    
    //字符串以开头比较
    if([str0 hasPrefix:@"just"])
    {
        NSLog(@"字符串str0以just开头");
    }
    
    //字符串以结尾比较
    if([str1 hasSuffix:@"coding"])
    {
        NSLog(@"str1字符串以coding结尾");
    }
}
 

//isEqualToString方法      
NSString *astring01 = @"This is a String!";  
NSString *astring02 = @"This is a String!";  
BOOL result = [astring01 isEqualToString:astring02];  
NSLog(@"result:%d",result);  
//isEqualToString方法    
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 isEqualToString:astring02];
NSLog(@"result:%d",result);
 

//compare方法(comparer返回的三种值)      
NSString *astring01 = @"This is a String!";  
NSString *astring02 = @"This is a String!";      
BOOL result = [astring01 compare:astring02] = = NSOrderedSame;      
NSLog(@"result:%d",result);      
//NSOrderedSame 判断两者内容是否相同  
//compare方法(comparer返回的三种值)    
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";    
BOOL result = [astring01 compare:astring02] = = NSOrderedSame;    
NSLog(@"result:%d",result);    
//NSOrderedSame 判断两者内容是否相同
 

NSString *astring01 = @"This is a String!";  
NSString *astring02 = @"this is a String!";  
BOOL result = [astring01 compare:astring02] = = NSOrderedAscending;      
NSLog(@"result:%d",result);  
//NSOrderedAscending 判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真)  
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"this is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedAscending;    
NSLog(@"result:%d",result);
//NSOrderedAscending 判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真)
 

NSString *astring01 = @"this is a String!";  
NSString *astring02 = @"This is a String!";  
BOOL result = [astring01 compare:astring02] = = NSOrderedDescending;      
NSLog(@"result:%d",result);      
//NSOrderedDescending 判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)  
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedDescending;    
NSLog(@"result:%d",result);    
//NSOrderedDescending 判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)
 

//不考虑大 小写比较字符串1  
NSString *astring01 = @"this is a String!";  
NSString *astring02 = @"This is a String!";  
BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;      
NSLog(@"result:%d",result);      
//NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为 真)  
//不考虑大 小写比较字符串1
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;    
NSLog(@"result:%d",result);    
//NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为 真)
 

//不考虑大小写比较字符串2  
NSString *astring01 = @"this is a String!";  
NSString *astring02 = @"This is a String!";  
BOOL result = [astring01 compare:astring02 options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame;      
NSLog(@"result:%d",result);      
//NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。  
//不考虑大小写比较字符串2
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02 options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame;    
NSLog(@"result:%d",result);    
//NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。
判断的方法可以利用 NSString 类别中 caseInsensitiveCompare: 所回传 -1、0 或是 1 的数值,判定两个字串之间得排序关系,其程式玛如下。

NSString *string = @"0";  
 NSComparisonResult result = [string caseInsensitiveCompare:@"A"];  
 switch (result) {  
 case NSOrderedAscending:  
 NSLog(@"升幂");  
 break;  
 case NSOrderedSame:  
 NSLog(@"忽略大小写相同的字串");  
 break;  
 case NSOrderedDescending:  
 NSLog(@"降幂");  
 break;  
 default:  
 NSLog(@"无法判定");  
 break;  
NSString *string = @"0";
 NSComparisonResult result = [string caseInsensitiveCompare:@"A"];

 switch (result) {
 case NSOrderedAscending:
 NSLog(@"升幂");
 break;

 case NSOrderedSame:
 NSLog(@"忽略大小写相同的字串");
 break;

 case NSOrderedDescending:
 NSLog(@"降幂");
 break;

 default:
 NSLog(@"无法判定");
 break;
 }
 

4. 文件字符串操作(读写)

//从文件读取字符串:initWithContentsOfFile方法  
NSString *path = @"astring.text";  
NSString *astring = [[NSString alloc] initWithContentsOfFile:path];  
NSLog(@"astring:%@",astring);  
[astring release]; //astring=nil;  
//从文件读取字符串:initWithContentsOfFile方法
NSString *path = @"astring.text";
NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
NSLog(@"astring:%@",astring);
[astring release]; //astring=nil;
 

//写字符串到文件:writeToFile方法  
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];  
NSLog(@"astring:%@",astring);  
NSString *path = @"astring.text";      
[astring writeToFile: path atomically: YES];  
[astring release];  //astring=nil;  
//写字符串到文件:writeToFile方法
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
NSLog(@"astring:%@",astring);
NSString *path = @"astring.text";    
[astring writeToFile: path atomically: YES];
[astring release];  //astring=nil;
 

* ios5 不支持release iOS应用开发:什么是ARC?

//扩展路径  
NSString *Path = @"~/NSData.txt";  
NSString *absolutePath = [Path stringByExpandingTildeInPath];  
NSLog(@"absolutePath:%@",absolutePath);  
NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);  
//扩展路径
NSString *Path = @"~/NSData.txt";
NSString *absolutePath = [Path stringByExpandingTildeInPath];
NSLog(@"absolutePath:%@",absolutePath);
NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
//文件扩展名  
NSString *Path = @"~/NSData.txt";  
NSLog(@"Extension:%@",[Path pathExtension]);  
//文件扩展名
NSString *Path = @"~/NSData.txt";
NSLog(@"Extension:%@",[Path pathExtension]);
 

5. 字符串的截取和大小写

//改变字符串的大小写  
NSString *string1 = @"A String";  
NSString *string2 = @"String";  
NSLog(@"string1:%@",[string1 uppercaseString]);//大写  
NSLog(@"string2:%@",[string2 lowercaseString]);//小写  
NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小  
//改变字符串的大小写
NSString *string1 = @"A String";
NSString *string2 = @"String";
NSLog(@"string1:%@",[string1 uppercaseString]);//大写
NSLog(@"string2:%@",[string2 lowercaseString]);//小写
NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小
 

Java代码  
- (void)viewDidLoad  
    [super viewDidLoad];  
    NSString *str0 = @"中文my name is xuanyusong";  
    //截取字符串起始点到index为4之间的内容  
    NSString * to = [str0 substringToIndex:4];  
    NSLog(@"to = %@",to);  
    //截取字符index为2 到结尾之间的内容  
    NSString * from = [str0 substringFromIndex:2];  
    NSLog(@"from = %@",from);  
    //设置截取字符串的范围   
    //从第二位开始,长度为十  
    NSRange rang = NSMakeRange(2, 10);  
    NSString * strRang = [str0 substringWithRange:rang];  
    NSLog(@"rang = %@",strRang);  
    //设置字符串首字母大写  
    NSLog(@"str0首字母大写:%@",[str0 capitalizedString]);  
    //设置字符串全部内容为大写  
    NSLog(@"str0大写:%@",[str0 uppercaseString]);  
    //设置字符全部内容为小写  
    NSLog(@"str0小写:%@",[str0 lowercaseString]);  
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    NSString *str0 = @"中文my name is xuanyusong";
    
    //截取字符串起始点到index为4之间的内容
    NSString * to = [str0 substringToIndex:4];
    
    NSLog(@"to = %@",to);
    
    //截取字符index为2 到结尾之间的内容
    NSString * from = [str0 substringFromIndex:2];
    
    NSLog(@"from = %@",from);
    
    
    //设置截取字符串的范围 
    //从第二位开始,长度为十
    NSRange rang = NSMakeRange(2, 10);
    NSString * strRang = [str0 substringWithRange:rang];
    NSLog(@"rang = %@",strRang);
    
    //设置字符串首字母大写
    NSLog(@"str0首字母大写:%@",[str0 capitalizedString]);
    //设置字符串全部内容为大写
    NSLog(@"str0大写:%@",[str0 uppercaseString]);
    //设置字符全部内容为小写
    NSLog(@"str0小写:%@",[str0 lowercaseString]);
    
}
 

6.搜索字符串与替换字符串

- (void)viewDidLoad  
    [super viewDidLoad];  
    NSString *str0 = @"中文my name is xuanyusong";  
    NSString *temp = @"is";  
    NSRange rang = [str0 rangeOfString:temp];  
    NSLog(@"搜索的字符串在str0中起始点的index 为 %d", rang.location);  
    NSLog(@"搜索的字符串在str0中结束点的index 为 %d", rang.location + rang.length);  
    //将搜索中的字符串替换成为一个新的字符串  
    NSString *str = [str0 stringByReplacingCharactersInRange:rang withString:@"哇咔咔卡卡咔"];   
    NSLog(@"替换后字符串为%@", str);  
    //将字符串中" " 全部替换成 *  
    str = [str0 stringByReplacingOccurrencesOfString :@" " withString:@"@"];   
    NSLog(@"替换后字符串为%@", str);  
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    NSString *str0 = @"中文my name is xuanyusong";
    NSString *temp = @"is";
    NSRange rang = [str0 rangeOfString:temp];
    
    NSLog(@"搜索的字符串在str0中起始点的index 为 %d", rang.location);
    NSLog(@"搜索的字符串在str0中结束点的index 为 %d", rang.location + rang.length);
    
    //将搜索中的字符串替换成为一个新的字符串
    NSString *str = [str0 stringByReplacingCharactersInRange:rang withString:@"哇咔咔卡卡咔"]; 
    NSLog(@"替换后字符串为%@", str);

    
    //将字符串中" " 全部替换成 *
    str = [str0 stringByReplacingOccurrencesOfString :@" " withString:@"@"]; 
    NSLog(@"替换后字符串为%@", str);
    
    
}
 

拓展:使用下面这个方法整体替换字符串还可以设置替换的区域。 

stringByReplacingOccurrencesOfString:(NSString *)withString:(NSString *) options:(NSStringCompareOptions) range:(NSRange)

7.字符串尾部添加

使用alloc在内存中创建字符串对象后边可以动态的操作这个字符串,修改与添加等。

appendstring 方法:向字符串尾部添加一个字符串。

appendFormat方法:向字符串尾部添加多个类型的字符串,可以添加任意数量与类型的字符串。

- (void)viewDidLoad  
    [super viewDidLoad];  
    NSMutableString * str = [[NSMutableString alloc] init];  
    //添加普通的字符串  
    [str appendString:@"aaa"];  
    //添加字符串 整型 字符型  
    [str appendFormat:@"我的名字:%@ 我的年龄:%d 我的邮箱:%s",@"justcoding", 25,"justcoding@gmail.com"];  
    NSLog(@"str = %@",str);  
- (void)viewDidLoad
{
    [super viewDidLoad];
    
   
    NSMutableString * str = [[NSMutableString alloc] init];
    
    //添加普通的字符串
    [str appendString:@"aaa"];
    
    //添加字符串 整型 字符型
    [str appendFormat:@"我的名字:%@ 我的年龄:%d 我的邮箱:%s",@"justcoding", 25,"justcoding@gmail.com"];
   
    NSLog(@"str = %@",str);
    
}
8. 字符串中删除元素

stringWithString 方法:用于创建字符串初始化赋值

rangeOfString方法:传入字符串返回一个在该字符串中的范围 也可以写 NSMakeRange(0, 3) 意思是范围在字符串0位到第3位

deleteCharactersInRange:删除字符串 参数为Range 就是删除的范围。

- (void)viewDidLoad  
    [super viewDidLoad];  
    //创建字符串  
    NSMutableString *str = [NSMutableString stringWithString:@"justcoding is best!"];  
    //删除字符串中含"justcoding"的字符  
    [str deleteCharactersInRange: [str rangeOfString: @"justcoding"]];      
    NSLog(@"str = %@",str);  
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //创建字符串
    NSMutableString *str = [NSMutableString stringWithString:@"justcoding is best!"];
    
    //删除字符串中含"justcoding"的字符
    [str deleteCharactersInRange: [str rangeOfString: @"justcoding"]];    
    
    NSLog(@"str = %@",str);
    
}
 

9. 字符串插入

stringWithString 方法:用于创建字符串初始化赋值

insertString方法:第一个参数 插入的字符串对象, 第二个参数 插入的位置。

- (void)viewDidLoad  
    [super viewDidLoad];  
    //创建字符串  
    NSMutableString *str = [NSMutableString stringWithString:@"justcoding is"];  
    //在str第10位插入字符串  
    [str insertString:@"best man" atIndex:10];  
    NSLog(@"str = %@",str);  
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //创建字符串
    NSMutableString *str = [NSMutableString stringWithString:@"justcoding is"];
    
    //在str第10位插入字符串
    [str insertString:@"best man" atIndex:10];
    
    NSLog(@"str = %@",str);
    
}
 

10. 字符串拷贝

- (void)viewDidLoad  
    [super viewDidLoad];  
    //创建字符串  
    NSMutableString *str1 = [NSMutableString stringWithString: @"字符串1"];  
    NSMutableString *str2;  
    //字符串赋值  
    str2 = str1;  
    [str2 appendString: @" 和字符串2"];  
    NSLog (@"str1 = %@", str1);  
    NSLog (@"str2 = %@", str2);      
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //创建字符串
    NSMutableString *str1 = [NSMutableString stringWithString: @"字符串1"];
    NSMutableString *str2;
    
    //字符串赋值
    str2 = str1;
    
    [str2 appendString: @" 和字符串2"];
    
    NSLog (@"str1 = %@", str1);
    
    NSLog (@"str2 = %@", str2);    
}
 

为什么给str2添加数据后 str1的数据也改变了?这就是指针的魅力所在,因为我们操作的是指针,str2 = str1  意思是两个指针同时指向一块内存,那么str2指向的内存内容改变以后str1当然跟着改变了。

11. 字符串与指定类型转换

如果转换的参数非法的话不会抛出异常,好比用中文去转整型。不会报错 但是转换结果为 0  ,默认值。

- (void)viewDidLoad  
    [super viewDidLoad];  
    //字符串转整型  
    NSString *str0 = @"1121";  
    //NSString *str0 = @"中国";  
    //把字符串强转成整型  
    int i = [str0 intValue];  
    NSLog (@"转换后:%i", i);   
    //字符串转interger  
      NSString *str1 = @"1985";  
    // NSString *str1 = @"中国";  
    //把字符串强转成interger  
    NSInteger ii = [str1 integerValue];  
    NSLog (@"转换后:%i", ii);   
    //字符串转double  
    NSString *str2 = @"3.145926";  
    //NSString *str2 = @"中国";  
    //把字符串强转成double  
    double d = [str2 doubleValue];  
    NSLog (@"转换后:%f", d);   
    //字符串转float  
    NSString *str3 = @"3.145926";  
     //NSString *str3 = @"中国";  
    //把字符串强转成float  
    double f = [str3 floatValue];  
    NSLog (@"转换后:%f", f);   
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //字符串转整型
    NSString *str0 = @"1121";
    //NSString *str0 = @"中国";
    
    //把字符串强转成整型
    int i = [str0 intValue];
    
    NSLog (@"转换后:%i", i); 
    
    //字符串转interger
      NSString *str1 = @"1985";
    // NSString *str1 = @"中国";
    
    //把字符串强转成interger
    NSInteger ii = [str1 integerValue];
    
    NSLog (@"转换后:%i", ii); 
    
    
    
    //字符串转double
    NSString *str2 = @"3.145926";
    //NSString *str2 = @"中国";
    
    //把字符串强转成double
    double d = [str2 doubleValue];
    
    NSLog (@"转换后:%f", d); 
    
    //字符串转float
    NSString *str3 = @"3.145926";
     //NSString *str3 = @"中国";
    //把字符串强转成float
    double f = [str3 floatValue];
    
    NSLog (@"转换后:%f", f); 
    
}

展开阅读全文

没有更多推荐了,返回首页