当前位置 : 主页 > 手机开发 > ios >

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

来源:互联网 收集:自由互联 发布时间:2021-05-10
IOS 字符串常用处理详细介绍 NSString *tempA = @"123"; NSString *tempB = @"456"; 1,字符串拼接 NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB]; 2,字符转int int intString = [newString intValue]; 3,

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

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

网友评论