iOS如何用一个字符串截取另一个字符串
2015-12-21 10:26
323 查看
有时候我们需要用一个字符串截取另一个字符串,但是用于截取的字符串A在被截取的字符串B中的位置又不固定。怎么办呢?
方法1:
//截取字符串stringAccount中除去字符串stringCom后的部分
NSString *stringAccount = @"china_1585436535@Yahoo.com.cn";
NSString *stringCom = @"@Yahoo.com.cn";
NSRange range = [stringAccount rangeOfString:stringCom];
NSString *string2 = [stringAccount substringToIndex:range.location];
NSLog(@"%@",string2);//输出 china_1585436535
方法2:
NSString *stringAccount = @"china_1585436535@Yahoo.com.cn";
NSString *retrunStr = [[stringAccount componentsSeparatedByString:@"@"] objectAtIndex:0];
//注:这种方法是利用字符串“@”来分割字符串stringAccount,得到两个字符串china_1585436535和Yahoo.com.cn 并且返回值是一个数组,前一个字符串存在index 0中,后一个存在1中
方法1:
//截取字符串stringAccount中除去字符串stringCom后的部分
NSString *stringAccount = @"china_1585436535@Yahoo.com.cn";
NSString *stringCom = @"@Yahoo.com.cn";
NSRange range = [stringAccount rangeOfString:stringCom];
NSString *string2 = [stringAccount substringToIndex:range.location];
NSLog(@"%@",string2);//输出 china_1585436535
方法2:
NSString *stringAccount = @"china_1585436535@Yahoo.com.cn";
NSString *retrunStr = [[stringAccount componentsSeparatedByString:@"@"] objectAtIndex:0];
//注:这种方法是利用字符串“@”来分割字符串stringAccount,得到两个字符串china_1585436535和Yahoo.com.cn 并且返回值是一个数组,前一个字符串存在index 0中,后一个存在1中
相关文章推荐
- iOS 时间状态栏的隐藏和显示 重载两个函数即可
- SDWebimage 加载大量图片的时候内存泄露的解决方法(ios自学笔记)
- iOS 23-自定义cell上的button点击事件.
- iOS NSTimer定时器
- 漫谈iOS程序的证书和签名机制
- iOS开发注意事项1
- 如何处理iOS中照片的方向
- iOS 容易造成循环引用的三种场景
- ios Label高度自适应
- iOS crash 信息符号
- iOS开发中常见的项目文件与MVC结构优化思路解析
- iOS一些常用动画总结
- ios 9 隐藏 StatusBar
- iOS 手势影响touch响应事件
- 2.Storyboard传参小技巧
- ios音效播放代码
- AA记账随手记ios项目源码
- 蜂窝教育:为你讲述iOS培训学员的梦想故事
- iOS之[打电话,发短信/邮件,打开URL...]
- iOS 中的观察者模式, KVO, 通知