欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS 字符串常用处理详细介绍

程序员文章站 2023-12-20 20:23:34
ios 字符串常用处理详细介绍 nsstring *tempa = @"123"; nsstring *tempb = @"456"; 1,字符串拼接...

ios 字符串常用处理详细介绍

nsstring *tempa = @"123";
 nsstring *tempb = @"456";

1,字符串拼接

 nsstring *newstring = [nsstring stringwithformat:@"%@%@",tempa,tempb];

2,字符转int

int intstring = [newstring intvalue];

3,int转字符

nsstring *stringint = [nsstring stringwithformat:@"%d",intstring];

4,字符转float

 float floatstring = [newstring floatvalue];

5,float转字符

nsstring *stringfloat = [nsstring stringwithformat:@"%f",intstring];

6.截取字符串

nsstring*string =@"sdfsfsfsadfsdf";
string = [string substringtoindex:7];//截取下标7之后的字符串
nslog(@"截取的值为:%@",string);
[string substringfromindex:2];//截取下标2之前的字符串
nslog(@"截取的值为:%@",string);

7.匹配字符串

nsstring*string =@"sdfsfsfsadfsdf";
nsrangerange = [stringrangeofstring:@"f"];//匹配得到的下标
nslog(@"rang:%@",nsstringfromrange(range));
string = [string substringwithrange:range];//截取范围类的字符串
nslog(@"截取的值为:%@",string);

8.分隔字符串

nsstring*string =@"sdfsfsfsadfsdf";
nsarray *array = [string componentsseparatedbystring:@"a"]; //从字符a中分隔成2个元素的数组
nslog(@"array:%@",array); //结果是adfsfsfs和dfsdf

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: