调用:
NSString *str1 = @"12342314.000000"
.deleteLastStr(@"0")
.deleteLastChar()
.addFloat(111.010825,3,true);
log : 12342314111.011
.h
//
// NSString+StringHandler.h
// OC_AttributedStringHandler
//
// Created by 李鹏跃 on 2018/9/28.
// Copyright © 2018年 13lipengyue. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface NSString (StringHandler)
/**
添加objc对象
· 参数为id类型
*/
- (NSString *(^)(id objc)) addObjc;
/**
返回拼接float后的字符串
· 第一个参数: float
· 第二个参数: 保留几位小数
· 第三个参数: 是否四舍五入
*/
- (NSString *(^)(CGFloat value,
NSInteger decimalDigits,
BOOL isRound)) addFloat;
/**
添加int字符
*/
- (NSString *(^)(NSInteger value)) addInt;
- (NSString *(^)(NSString *deleteStr)) deleteStr;
/**
* @brief 从后开始遍历删除指定字符
* @ 参数为 string
* @return 返回str.
*/
- (NSString *(^)(NSString *deleteStr)) deleteLastStr;
/**
* @brief 从前开始遍历删除指定字符
* @ 参数为 string
* @return 返回str.
*/
- (NSString *(^)(NSString *deleteStr)) deleteFrontStr;
/**
* @brief 删除最后一个字符 @ 参数为 void
*/
- (NSString *(^)(void)) deleteLastChar;
/**
* @brief 删除第一个字符 @ 参数为 void
*/
- (NSString *(^)(void)) deleteFrontChar;
/**
float 转成字符串
@param value float值
@param decimalDigits 保留几位小数
@param isRound 四舍五入或者直接去掉最后的小数
@return string
*/
+ (NSString *) createWithFloat: (CGFloat)value
andDecimalDigits: (NSInteger)decimalDigits
andIsRound: (BOOL)isRound;
+ (NSString *) createWithInt: (NSInteger)value;
@end
.m
//
// NSString+StringHandler.m
// OC_AttributedStringHandler
//
// Created by 李鹏跃 on 2018/9/28.
// Copyright © 2018年 13lipengyue. All rights reserved.
//
#import "NSString+StringHandler.h"
#import <math.h>
@implementation NSString (StringHandler)
- (NSString *(^)(id objc)) addObjc {
return ^(id objc){
return [NSString stringWithFormat:@"%@%@",self,objc];
};
}
- (NSString *(^)(CGFloat value, NSInteger decimalDigits, BOOL isRound)) addFloat {
return ^(CGFloat value, NSInteger decimalDigits, BOOL isRound) {
NSString *floatValue = [NSString createWithFloat:value
andDecimalDigits:decimalDigits
andIsRound:isRound];
return [NSString stringWithFormat:@"%@%@",self,floatValue];
};
}
- (NSString *(^)(NSInteger value)) addInt {
return ^(NSInteger value) {
return [NSString stringWithFormat:@"%ld",value];
};
}
- (NSString *(^)(NSString *deleteStr)) deleteStr {
return ^ (NSString *deleteStr) {
NSMutableString *str = self.mutableCopy;
[str stringByReplacingOccurrencesOfString:deleteStr withString:@""];
return str;
};
}
- (NSString *(^)(NSString *deleteStr)) deleteLastStr {
return ^ (NSString *deleteStr) {
NSMutableString *str = self.mutableCopy;
NSInteger i = 0, length = str.length;
for (i = length - 1; i >= 0; i--) {
NSString *lastStr = [str substringFromIndex:str.length - 1];
if ([lastStr isEqualToString:deleteStr]) {
NSRange range = NSMakeRange(i, 1);
[str deleteCharactersInRange:range];
}else{ break; }
}
return str;
};
}
- (NSString *(^)(NSString *deleteStr)) deleteFrontStr {
return ^ (NSString *deleteStr) {
NSMutableString *str = self.mutableCopy;
NSInteger i, length = str.length;
NSString *firstStr = [str substringFromIndex:0];
for (i = 0; i < length; i++) {
if ([firstStr isEqualToString:deleteStr]) {
NSRange range = NSMakeRange(0, 1);
[str deleteCharactersInRange:range];
}
}
return str;
};
}
- (NSString *(^)(void)) deleteLastChar {
return ^ {
NSMutableString *str = self.mutableCopy;
NSRange range = NSMakeRange(self.length-1, 1);
[str deleteCharactersInRange:range];
return str;
};
}
- (NSString *(^)(void)) deleteFrontChar {
return ^ {
NSMutableString *str = self.mutableCopy;
NSRange range = NSMakeRange(0, 1);
[str deleteCharactersInRange:range];
return str;
};
}
+ (NSString *) createWithFloat: (CGFloat)value
andDecimalDigits: (NSInteger)decimalDigits
andIsRound: (BOOL)isRound {
NSInteger decimalDigitsTemp = decimalDigits;
CGFloat floatValue = value * (pow(10,decimalDigitsTemp));
NSInteger integerValue = 0;
integerValue = isRound ? round(floatValue) : floatValue;
NSMutableString *strTemp = @"".mutableCopy;
for (int i = 0; i < decimalDigitsTemp; i ++) {
NSInteger value = integerValue % 10;
NSString *valueStr = [NSString stringWithFormat:@"%ld",value];
[strTemp insertString:valueStr atIndex:0];
integerValue /= 10;
}
integerValue = value;
return [NSString stringWithFormat:@"%ld.%@",integerValue,strTemp];
}
+ (NSString *) createWithInt: (NSInteger)value {
return [NSString stringWithFormat:@"%ld",value];
}
@end
网友评论