OC中的NSNumber、NSArray、NSString的常用方法

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

和C语言不同,在Objective-C语言中,有单独的字符串类NSString。C语言中,string是由 char(ASCLL码)字符组成

OC中,字符串是由unichar(Unicode)字符组成

NSString,不可变字符串,即:创建以后,内容和长度不能更改

NSMutableString,可变字符串,即:创建以后,内容还可以修改

在使用哟个字符串对象之前首先创建一个新的字符串,可以使用实例方法和便利构造器

NSString常用的方法

1、使用实例方法和便利构造器创建一个新的字符串

2、获取字符串长度

3、获取子字符串

4、拼接字符串

5、替换字符串

6、字符串相等

7、字符串比较

使用初始化方法创建

NSString *str1 = [[NSString alloc] initWithString:@"name"];

NSLog(@"%@",str1);

NSString *str11 = @"name";

NSLog(@"%@",str11);

使用实例方法创建

NSString *str2 = [NSString stringWithString:@"name"];

NSLog(@"%@",str2);

NSString *str22 = @"name";

NSLog(@"%@",str22);

char *cStr= "hehe";

将c语言字符串转成OC的对象

NSString *str3 = [[NSString alloc] initWithCString:cStr encoding:NSUTF8StringEncoding];

NSLog(@"%@",str3);

NSString *str4 = [NSString stringWithCString:cStr encoding:NSUTF8StringEncoding];

NSLog(@"%@",str4);

根据指定格式创建字符串

NSString *str5 = [[NSString alloc] initWithFormat:@"%@+%d",@"duke",1001];

NSLog(@"%@",str5);

NSString *str6 = [NSString stringWithFormat:@"%@+%d",@"duke",1001];

NSLog(@"%@",str6);

根据指定路径的文件内容创建字符串对象

NSString *str7 = [[NSString alloc] initWithContentsOfFile:@"/Users/lanouhn/Desktop/未命名.txt" encoding:NSUTF8StringEncoding error:nil];

NSLog(@"%@",str7);

NSString *str8 = [NSString stringWithContentsOfFile:@"/Users/lanouhn/Desktop/未命名.txt" encoding:NSUTF8StringEncoding error:nil];

NSLog(@"%@",str8);

求字符串对象的长度

NSInteger length = [str8 length];

NSLog(@"%ld",length);

判断一个字符串是否拥有前缀字符串

BOOL result1 = [str8 hasprefix:@"李"];

NSLog(@"%@",result1 ? @"YES" : @"NO");

判断一个字符串是否拥有后缀字符串

BOOL result2 = [str8 hasSuffix:@"李"];

NSLog(@"%@",result2 ? @"YES" : @"NO");

判断两个字符串是否相同

BOOL result3 = [str8 isEqualToString:str7];

NSLog(@"%@",result3 ? @"YES" : @"NO");

字符串比较排序结果

NSComparisonResult result4 = [str8 compare:str7];

NSLog(@"%ld",result4);//升序为-1,降序为1,相同为0

获取子字符串

从指定下标(包含指定下标)到字符串结束的子字符串

NSString *subStr1 = [str8 substringFromIndex:2];

NSLog(@"%@",subStr1);

从字符串开始到指定下标的字符(不包含指定下标)的子字符串

NSString *subStr2 = [str8 substringToIndex:2];

NSLog(@"%@",subStr2);

NSRange为结构体类型,成员location描述下标位置,成员length描述需要截取的子字符串长度

NSRange range = NSMakeRange(1, 3);

// NSRange range = {1,3};

NSString *subStr3 = [str8 substringWithRange:range];

NSLog(@"%@",subStr3);

字符串拼接

根据给定的参数字符串拼接产生新的字符串,不改变原有的字符串

NSString *newString1 = [str8 stringByAppendingString:@"叉1001"];

NSLog(@"%@",newString1);

根据指定的格式串以及参数去拼接产生新的字符串

NSString *newString2 = [str8 stringByAppendingFormat:@"%d",1001];

NSLog(@"%@",newString2);

路径拼接

NSString *newString3 = [str8 stringByAppendingPathComponent:@"xx.avi"];

NSLog(@"%@",newString3);

字符串的替换

通过给定的第二个字符串替换str8中当前存在的字符串

NSString *newString4 = [str8 stringByReplacingOccurrencesOfString:@"李X" withString:@"无双"];

NSLog(@"%@",newString4);

查找字符串

NSString *link = @"abdjofepok = _nieifn";

NSRange range1 = [link rangeOfString:@"pok = _nie"];

NSLog(@"%@",NSStringFromRange(range1));

if (range1.location != NSNotFound) {

NSLog(@"founded");

}

字符串与数值类数据的转换

NSString *numString1 = @"1";

NSInteger integerValue = [numString1 integerValue];

NSLog(@"%ld",integerValue);

大小写转换

NSString *string = @"i love you";

转成大写

NSString *upperCaseStr = [string uppercaseString];

NSLog(@"%@",upperCaseStr);

转成小写字符串

NSString *lowCaseStr= [upperCaseStr lowercaseString];

NSLog(@"%@",lowCaseStr);

转成首字母大写字符串

NSString *capitalString = [string capitalizedString];

NSLog(@"%@",capitalString);

NSMutableString(可变字符串)

NSMutableString是NSString的子类,通过NSMutableString创建的字符串是一个动态的可变的字符串,可以对字符串进行增删改等操作

常用方法包括:

创建一个新的字符串

拼接字符串

插入字符

删除字符

NSMutableString *mutableStr1 = [[NSMutableString alloc] init];

NSLog(@"%@",mutableStr1);

NSMutableString *mutableStr2 = [NSMutableString string];

可变字符串的拼接

stringByAppendingString

[mutableStr1 appendString:@"abcdeg"];

NSLog(@"%@",mutableStr1);

NSString *resultString = [mutableStr1 stringByAppendingString:@"xxxxx"];

NSLog(@"%@",mutableStr1);

NSLog(@"%@",resultString);

这种字符串拼接,不改变原来的对象

另一个字符串拼接方法

stringByAppendingFormat

[mutableStr2 appendFormat:@"duke + %d",1001];

NSLog(@"%@",mutableStr2);

删除字符串

[mutableStr2 deleteCharactersInRange:NSMakeRange(4,6)];

NSLog(@"%@",mutableStr2);

插入字符串

在给定的下标之前插入新的字符串

[mutableStr2 insertString:@"heheh" atIndex:0];

NSLog(@"%@",mutableStr2);

替换字符串

根据给定的字符串替换指定范围的字符门

[mutableStr2 replaceCharactersInRange:NSMakeRange(0, 5) withString:@"hehe"];

NSLog(@"%@",mutableStr2);

下面是一个实例分别通过不可变字符方法和可变方法去解答

给定一个图片文件名,判断字符串中是否以“png”结尾,如果是就替换成“jpg”,如果 不是,就拼接”.jpg”。

不可变字符串

NSString *picName = [NSString stringWithFormat:@"image.png"];

NSString *resultStr = nil;

if ([picName hasSuffix:@"png"]) {

resultStr = [picName stringByReplacingOccurrencesOfString:@"png" withString:@"jpg"];

} else {

resultStr = [picName stringByAppendingString:@".jpg"];

}

NSLog(@"%@",resultStr);

可变字符串

NSMutableString *picture = [NSMutableString stringWithString:picName];

if ([picture hasSuffix:@"png"]) {

[picture replaceCharactersInRange:[picture rangeOfString:@"png"] withString:@"jpg"];

} else {

[picture appendString:@".jpg"];

}

NSLog(@"%@",picture);

OC中存放数据的容器类都称为集合(collection)

数组是有序集合,只能存放对象

数组有下标(index)的概念,靠index来索引元素,下标从0开始

数组分不可变数组(NSArray)和可变数组(NSMutableArray)

常用的方法是:

创建数组对象,使用实例初始化或便利构造器

获取元素个数

根据index获取对象

//定义NSArray

NSArray *array1 = [[NSArray alloc] initWithObjects:@"1",@2,@"哈哈",nil];

NSLog(@"%@",[array1 description]);

NSArray *array2 = [NSArray arrayWithObjects:@"1",@2,@"☺",nil];

NSLog(@"%@",array2);

//数组的语法糖形式 (literal,字面量)

NSArray *array3 = @[@"1",@2,@"☺"];

NSLog(@"%@",array3);

//获取数组元素个数

NSInteger count = [array3 count];

NSLog(@"%ld",count);

//通过下标获取对应的对象

for (int i = 0; i < [array3 count]; i++) {

// NSLog(@"%@",[array3 objectAtIndex:i]);

NSLog(@"%@",array3[i]);

}

//通过对象去查找他在数组中的下标

NSInteger index = [array3 indexOfObject:@2];

NSLog(@"%ld",index);

NSLog(@"----------------------------------");

NSString *textString = [NSString stringWithContentsOfFile:@"/Users/Duke/Desktop/未命名.txt" encoding:NSUTF8StringEncoding error:nil];

NSLog(@"%@",textString);

//通过给定的字符串将原有字符串截取成多个子字符串并保存在数组中返回

NSArray *array4 = [textString componentsSeparatedByString:@"\n"];

NSLog(@"%@",array4);

//可变数组的使用--------------------------------------------------------

可变数组是NSArray的子类,继承NSArray的所有方法

可以对数组进行增删改等操作

常用的方法有:

创建数组对象

添加元素、插入元素

删除元素、替换元素

交换指定位置的两个元素

NSMutableArray *mutablearray1 = [[NSMutableArray alloc] initWithArray:array1];

NSLog(@"%@",mutablearray1);

NSMutableArray *mutableArray2 = [NSMutableArray arrayWithArray:array1];

NSLog(@"%@",mutableArray2);

//添加元素

[mutableArray2 addObject:@33];

NSLog(@"%@",mutableArray2);

//插入元素

[mutableArray2 insertObject:@123 atIndex:2];

NSLog(@"%@",mutableArray2);

//替换一个已有元素

[mutableArray2 replaceObjectAtIndex:2 withObject:@"heihei"];

NSLog(@"%@",mutableArray2);

//交换两个对应下标的对象的位置

[mutableArray2 exchangeObjectAtIndex:2 withObjectAtIndex:0];

NSLog(@"%@",mutableArray2);

//删除最后一个对象

[mutableArray2 removeLastObject];

NSLog(@"%@",mutableArray2);

//删除指定元素

[mutableArray2 removeObject:@2];

NSLog(@"%@",mutableArray2);

//删除指定下标的对象

[mutableArray2 removeObjectAtIndex:0];

NSLog(@"%@",mutableArray2);

//删除多个内容

//删除数组中的所有对象

[mutableArray2 removeAllObjects];

NSLog(@"%@",mutableArray2);

//遍历数组

NSArray *array = [NSArray arrayWithObjects:@"one",@"two",@"three",@"four", nil];

for (int index = 0; index < [array count]; index++) {

NSString *string = [array objectAtIndex:index];

NSLog(@"%@",string);

}

NSLog(@"-----------------------");

for (NSString *string in array) {

NSLog(@"%@",string);

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航