字符串数值计算
//价格字符串转成NSDecimalNumber类型
NSDecimalNumber *objA = [NSDecimalNumber decimalNumberWithString:@"18.22"];
NSDecimalNumber *objB = [NSDecimalNumber decimalNumberWithString:@"2.30"];
//1、两个字符串相乘
NSDecimalNumber *objC = [objA decimalNumberByMultiplyingBy:objB];
//2、两个字符串相除
NSDecimalNumber *objD = [objA decimalNumberByDividingBy:objB];
//3、两个字符串相加
NSDecimalNumber *objE = [objA decimalNumberByAdding:objB];
//输出结果
NSLog(@"objC:%@,objD:%@",[objC stringValue],[objD stringValue]);
去除字符串两端的空格、用户连续输入换行过滤、字符串包含替换
//去除字符串两端的空格
NSString *textString = [self.userString.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//换行过滤
while ([textViewTextString containsString:@"\n\n"]) {
textViewTextString = [textViewTextString stringByReplacingOccurrencesOfString:@"\n\n" withString:@"\n"];
}
//字符串包含替换
if ([pictureModel.picture_name containsString:@"200-200"]) {
NSString *tempPictureName = [pictureModel.picture_name stringByReplacingOccurrencesOfString:@"200-200" withString:@"500-500"];
}
拼接字符串,可以增加空格
cell.detailTextLabel.text= [datamodel.day stringByAppendingFormat:@" %@",datamodel.teacher];
字符串截取
NSString *a = @"abcde";
NSString *b = [a substringFromIndex:1];
NSLog(@"b--%@",b); //打印结果:bcde
NSString *c = [a substringToIndex:1];
NSLog(@"c--%@",c); //打印结果:a
//截取时间指定位置,2016年12月15日09:48:08 结果:09:48
NSString *cutTime = [stringTime substringWithRange:NSMakeRange(11,5)];
字符串中出现的某一个字符串次数
int count = 0;
NSString * string1 = @"1237847878712378798791231234";
NSString * string2 = @"123";
for (int i = 0; i < string1.length - string2.length + 1; i++) {
if ([[string1 substringWithRange:NSMakeRange(i, string2.length)] isEqualToString:string2]) {
count++;
NSLog(@"count == %d",count);
}
}
获取字符串后缀名:详解讲解地址
NSString * urlString = @”http://www.baidu.com/img/10.gif”;
NSString *fileName = [urlString lastPathComponent]; //10.gif
手机号码隐藏中间位数(4位)
NSString *str = payModel.username;
self.username.text = [str stringByReplacingCharactersInRange:NSMakeRange(4, 4) withString:@"xxxx"];
字符串截取4位数 每4位后面加空格(1234567845235252)
NSString *newStr =@"1234567845235252";
NSMutableString *temp = [NSMutableString string];
for(int i =0; i < newStr.length; i++)
{
NSString *str = [newStr substringWithRange:NSMakeRange(i, 1)];
[temp appendString:str];
if ((i+1) % 4 == 0) {
[temp appendString:@" "];
}
}
//打印:1234 5678 4523 5252
字符串按多个符号分割,打印结果
NSString *str = @"abnfd,fdffds,fdsfsdf.dsds";
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@",."];
NSArray *tempArray = [str componentsSeparatedByCharactersInSet:set];
NSLog(@"%@",tempArray);
//判断字符串是否空值,添加到NSString扩展类中
+ (BOOL) isBlankString:(NSString *)string {
//判断是否返回nil,返回(null)
if (string == nil || string == NULL || [string isEqualToString:@"(null)"]) {
return YES;
}
//判断是否返回为类
if ([string isKindOfClass:[NSNull class]]) {
return YES;
}
//判断是否返回空格
if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
return YES;
}
return NO;
}
勤学如早春之苗,不见其增,日有所涨。
辍学如磨刀之石,不见其减,日有所损。
网友评论