iOS字符串替换,字符插入,字符删除
2016-05-05 17:24
441 查看
1.字符串替换,(删除也可以用空白替换)
方法:- stringByReplacingOccurrencesOfString:withString:options:range:
样例:
NSString * string=@"2016-05-05";
string=[string stringByReplacingOccurrencesOfString:@"-"withString:@"/"];
NSLog(@"replaceStr=%@",string);
输入结果如下
replaceStr=2016/05/05
2.字符串插入
方法
- (void)insertString:(NSString
*)aString
atIndex:(NSUInteger)anIndex
样例:
+(NSString*)addSeparatorForPriceString:(NSString*)priceStr
{
NSString* price =
@"3000000";
NSMutableString* tempStr = price.mutableCopy;
NSRange range = [price
rangeOfString:@"."];
NSInteger index =
0;
if (range.length>0)
{
index = range.location;
}
else
{
index = price.length;
}
while ((index-3)>0)
{
index-=3;
[tempStr insertString:@","
atIndex:index];
}
tempStr = [tempStr stringByReplacingOccurrencesOfString:@"."
withString:@","].mutableCopy;
return tempStr;
}
方法:- stringByReplacingOccurrencesOfString:withString:options:range:
样例:
NSString * string=@"2016-05-05";
string=[string stringByReplacingOccurrencesOfString:@"-"withString:@"/"];
NSLog(@"replaceStr=%@",string);
输入结果如下
replaceStr=2016/05/05
2.字符串插入
方法
- (void)insertString:(NSString
*)aString
atIndex:(NSUInteger)anIndex
样例:
+(NSString*)addSeparatorForPriceString:(NSString*)priceStr
{
NSString* price =
@"3000000";
NSMutableString* tempStr = price.mutableCopy;
NSRange range = [price
rangeOfString:@"."];
NSInteger index =
0;
if (range.length>0)
{
index = range.location;
}
else
{
index = price.length;
}
while ((index-3)>0)
{
index-=3;
[tempStr insertString:@","
atIndex:index];
}
tempStr = [tempStr stringByReplacingOccurrencesOfString:@"."
withString:@","].mutableCopy;
return tempStr;
}
相关文章推荐
- iOS开发动画(Animation)图片360度不停旋转
- iOS的枚举
- iOS中atomic 与 nonatomic,assign,copy与retain的定义和区别
- iOS获取当前年、月、日等信息
- tableView的系统删除和点击删除
- IOS 杂笔-19(属性与变量的优缺点)
- iOS 搜索功能的实现
- IOS 警告 汇总
- iOS 清除缓存功能详细代码 可过滤特不删除特殊格式文件
- iOS 连个应用之间的跳转
- iOS ViewController点击空白处收起键盘
- iOS 统计代码行数
- iOS - 支付宝支付详解
- iOS 开发中 Duplicate interface definition for class'*'
- iOS 多线程--1
- iOS 设置Label的行间距
- iOS 同一个View识别单击和双击手势
- iOS开发 ☞ 上架常见问题
- iOS多线程 - GCD
- iOS 搜索功能的实现