NSString方法

2017-03-20  本文已影响55人  block_smile

1.对象方法

1 获取oc字符串长度

NSString *str = @"你好阿zhangsan";

NSInteger len = [str length];//可以用点语法 str.length

NSLog(@"%lu", len);

2 比较字符串是否相等(比较的是内容)

NSString *str = @"你好阿zhangsan";

NSString *str2 = @"zhangsan你好啊";

BOOL res = [str isEqualToString:str2];

NSLog(@"%d", res);

3 比较字符串大小(ASCII码值)

NSString *str = @"zhangsan";

NSString *str2 = @"zhangsan你好啊";

int res = [str compare:str2];

NSLog(@"%d", res);

4 获取指定下标的字符

NSString *str2 = @"zhangsan你好啊";

unichar ch = [str2 characterAtIndex:9];

NSLog(@"%C", ch);

5 将OC字符串转换为C字符串(不常用)

NSString *str = @"jack";

const char *name =  [str UTF8String];

NSLog(@"name = %s",name);

6 比较字符串中的数字的大小.

比较的是字符串中数字的大小.

使用前提: 两个字符串的格式要相同.一般情况下用来比较两个路径

NSString *str1 = @"img100021.jpg";

NSString *str2 = @"img100012.jpg";

int len =  [str1 compare:str2 options: NSNumericSearch];

NSLog(@"%d",len);

7 完全匹配的比较.

NSString *str1 = @"jack";

NSString *str2 = @"JACK";

int len =  [str1 compare:str2 options: NSLiteralSearch];

NSLog(@"%d",len);

8 忽略字母大小写的比较.

NSString *str1 = @"jack";

NSString *str2 = @"JACK";

int len =  [str1 compare:str2 options:NSCaseInsensitiveSearch];

NSLog(@"%d",len);

9 判断字符串是否以指定的字符串开始的.

NSString* str = @"nfsjbfalksnfjibfjas";

int result = [str hasPrefix:@"nf"];

NSLog(@"%d",i);

10 判断字符串是否以指定的字符串结束的.

NSString* str = @"nfsjbfalksnfjibfjas";

int result = [str hasSuffix:@"jas"];

NSLog(@"%d",result);

11 在字符串中搜索子字符串的范围 NSRange是一个数据类型

NSString* str = @"nfsjbfalksnfjibfjas";

NSRange  ran = [str rangeOfString:@"sjb"];

NSLog(@"%lu",ran.length);//length: 代表匹配的长度.

NSLog(@"%lu",ran.location);//location:代表这段范围的起始下标

12 把指定元素替换成另外的

NSString* str = @"我爱程序员哈哈!";

NSString* str1 = [str  stringByReplacingOccurrencesOfString:@"爱" withString:@"喜欢"];

NSLog(@"%@",str1);

13 还可以实现删除的效果: 将指定的字符串换成@""

NSString* str = @"我爱程序员哈哈!";

NSString* str1 = [str  stringByReplacingOccurrencesOfString:@"爱" withString:@""];

NSLog(@"%@",str1);

注意的是: 各种操作字符串的方法.原来字符串本身不会变.因为字符串的恒定性.

最终的操作结果都是以返回值返回来的

14 从指定下标截取指定范围,(包括下标)

NSString* str = @"我爱程序员哈哈!";

NSString* str1 =  [str substringWithRange:NSMakeRange(2, 3)];

NSLog(@"%@",str1);

15 从开始截取到指定下标(不包括下标)

NSString* str = @"我爱程序员哈哈!";

NSString* str1 =  [str substringToIndex:2];

NSLog(@"%@",str1);

16 从指定下标截取到最后(包括下标)

NSString* str = @"我爱程序员哈哈!";

NSString* str1 =  [str substringFromIndex:3];

NSLog(@"%@",str1);

17 只能删除前后的空格,中间的空格如果要删除就是以替换.

NSString *str = @"  chiwcniwnciwe  c winciwn ciewnciewc  fcneivbcnei      ";

str =  [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

NSLog(@"%@",str);

18 字符串类型转换  将字符串数据转换为别的类型.

//@property (readonly) double doubleValue;

//@property (readonly) float floatValue;

//@property (readonly) int intValue;

//@property (readonly) NSInteger integerValue;

//@property (readonly) long long longLongValue;

//@property (readonly) BOOL boolValue;

NSString *str = @"123kkkk123a1aa";

double d1 = [str doubleValue];

NSLog(@"%lf",d1);

如果字符串中的数据不完全是这个要转换的类型.

从头开始,能转换多少就是多少

19 把指定字符串写道指定地址的文件里

NSString* str = @"你好,我想日你呦!";

NSError* err = nil;

BOOL res = [str writeToFile:@"/aaUsers/mac/Desktop/abc.txt" atomically:NO encoding:NSUTF8StringEncoding error:&err];

if(err == nil)

{

NSLog(@"已保存");

}

else

{

NSLog(@"不能保存");

NSLog(@"%@",err.localizedDescription);

}

//第二种判断

if(res == YES)

{

NSLog(@"已保存");

}

else

{

NSLog(@"不能保存");

}

20 往NSURL中写入数据.

NSString* str1 = @"哈哈哈";

BOOL res = [str1 writeToURL:url3 atomically:NO encoding:NSUTF8StringEncoding error:nil];

2.类方法

1 将c字符串转换成oc字符串

char * ch = "hello";

NSString *str = [NSString stringWithUTF8String:ch];

NSLog(@"%@", str);

2 将N个变量拼接成一个oc字符串(不能转中文,否则失败)

NSString *name = @"xxx";

int age = 20;

NSString *str = [NSString stringWithFormat:@"我叫%@,我今年%d岁", name, age];

NSLog(@"%@", str);

3 从指定的文件中读取内容

NSError* err = nil;

NSString* str = [NSString stringWithContentsOfFile:@"/Users/mac/Desktop/abc.txt" encoding:NSUTF8StringEncoding error:&err];

NSLog(@"%@",str);

4 使用类方法从NSURL对象中读取数据.

NSString* str = [NSString stringWithContentsOfURL: url3 encoding:NSUTF8StringEncoding error:&err];

NSLog(@"%@",str);

//    字符串对象不仅可以从磁盘上读取文件的内容.

//    还可以从网络上读写数据.

//    可以读取1个网页的源代码.ftp服务器上的文件也可以读取.

//    网页地址,ftp文件地址.

//    URL 统一资源路径. 也就是1个网址.ftp文件的地址. 磁盘上的文件的地址.

//    网址的URL标准写法: http://www.itheima.com

//    ftp文件的URL标准写法: ftp://server.itheima.com/aa.avi

//    磁盘文件路径:  file:///Users/Itcast/Desktop/abc.txt

//    NSString可以从1个URL路径中读写数据.

//    1). 先将URL路径封装在1个NSURL对象中

NSURL *url1 = [NSURL URLWithString:@"http://www.itheima.com"];

NSURL *url2 = [NSURL URLWithString:@"ftp://server.itheima.com/1.txt"];

NSURL *url3 = [NSURL URLWithString:@"file:///Users/mac/Desktop/abc.txt"];

NSError* err = nil;

//    2). 使用类方法从NSURL对象中读取数据.

NSString* str = [NSString stringWithContentsOfURL: url3 encoding:NSUTF8StringEncoding error:&err];

NSLog(@"%@",str);

NSString* str1 = @"哈哈哈";

//    3). 往NSURL中写入数据.

BOOL res = [str1 writeToURL:url3 atomically:NO encoding:NSUTF8StringEncoding error:nil];

3.NSMutableString方法

NSString* s1 = @"你还好吗";

NSString* s2 = @"我好想你";

NSString* s3 = @"还爱我吗";

NSMutableString* str = [NSMutableString new];

//1 拼接的方式追加

[str appendFormat:@"%@", s1];

[str appendFormat:@"%@", s2];

[str appendFormat:@"%@", s3];

//2 追加字符串

[str appendString:s1];

[str appendString:s2];

[str appendString:s3];

NSLog(@"%@",str);

//3 将可变字符串转成不可变字符串 copy方法

NSString* str1 = [str copy];

NSLog(@"%@",str1);

上一篇 下一篇

猜你喜欢

热点阅读