iOS 关于NSString的一些方法
在项目中整理的一些关于字符串应用方法,可以全部封装在一个类里面进行调用,会不断更新添加:
1.数字转换成对应的中文数字(项目中课程分级目录的章节号用到)
摘自:http://blog.csdn.net/it_ds/article/details/47128563
+(NSString *)translation:(NSString *)arebic { NSString *str = arebic; NSArray *arabic_numerals = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0"]; NSArray *chinese_numerals = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"零"]; NSArray *digits = @[@"个",@"十",@"百",@"千",@"万",@"十",@"百",@"千",@"亿",@"十",@"百",@"千",@"兆"]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:chinese_numerals forKeys:arabic_numerals]; NSMutableArray *sums = [NSMutableArray array]; for (int i = 0; i < str.length; i ++) { NSString *substr = [str substringWithRange:NSMakeRange(i, 1)]; NSString *a = [dictionary objectForKey:substr]; NSString *b = digits[str.length -i-1]; NSString *sum = [a stringByAppendingString:b]; if ([a isEqualToString:chinese_numerals[9]]) { if([b isEqualToString:digits[4]] || [b isEqualToString:digits[8]]) { sum = b; if ([[sums lastObject] isEqualToString:chinese_numerals[9]]) { [sums removeLastObject]; } }else { sum = chinese_numerals[9]; } if ([[sums lastObject] isEqualToString:sum]) { continue; } } [sums addObject:sum]; } NSString *sumStr = [sums componentsJoinedByString:@""]; NSString *chinese = [sumStr substringToIndex:sumStr.length-1]; NSLog(@"%@",str); NSLog(@"%@",chinese); return chinese; }
2.判断是否含有中文(视频网址判断是否需要转码)
+(BOOL)isChinese:(NSString *)str{ for (int i=0; i<str.length; ++i) { NSRange range = NSMakeRange(i, 1); NSString *subString = [str substringWithRange:range]; const char *cString = [subString UTF8String]; if (strlen(cString) == 3) { return YES; } } return NO; }
3.去除特殊字符(去除时间戳中的”/“时用到)
+(NSString *) stringDeleteString:(NSString *)str { NSMutableString *str1 = [NSMutableString stringWithString:str]; for (int i = 0; i < str1.length; i++) { unichar c = [str1 characterAtIndex:i]; NSRange range = NSMakeRange(i, 1); if ( c == '/' || c == '-' || c == '.' || c == ',' || c == '(' || c == ')') { //此处可以是任何字符 [str1 deleteCharactersInRange:range]; --i; } } NSString *newstr = [NSString stringWithString:str1]; return newstr; }
4.特殊字符转义(textView往接口传入含”&“的内容不能成功 因为接口参数以”&“开头 解决方式是移动端将输入内容先转义 然后后台再转一次)
+(NSString *)replaceMyString:(NSString *)textStr{ // NSString *inputString = [[NSString alloc]init]; NSString *inputString = textStr; //inputString = [inputString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; inputString = [inputString stringByReplacingOccurrencesOfString:@"&" withString:@"&"]; inputString = [inputString stringByReplacingOccurrencesOfString:@"<" withString:@"<"]; inputString = [inputString stringByReplacingOccurrencesOfString:@">" withString:@">"]; inputString = [inputString stringByReplacingOccurrencesOfString:@" " withString:@" "]; inputString = [inputString stringByReplacingOccurrencesOfString:@"\'" withString:@"'"]; inputString = [inputString stringByReplacingOccurrencesOfString:@"\"" withString:@"""]; inputString = [inputString stringByReplacingOccurrencesOfString:@"\r" withString:@""]; inputString = [inputString stringByReplacingOccurrencesOfString:@"\n" withString:@"<br />"]; return inputString; }
5.将秒数转换成时、分(接口返回的时长都是秒单位 如3600 需要转成小时和分钟的样式时用到)
//修改时分的方法 +(NSString *)TimeformatFromSeconds:(int)seconds { if (seconds > 60 ) { NSString *str_hour = [NSString stringWithFormat:@"%d",seconds/3600]; NSString *str_minute = [NSString stringWithFormat:@"%d",(seconds%3600)/60]; if([str_hour isEqualToString:@"0"] ) { int minutInt = (seconds%3600)%60; if (minutInt != 0) { NSString *format_time = [NSString stringWithFormat:@"%d",[str_minute intValue]+1]; return format_time; }else{ NSString *format_time = [NSString stringWithFormat:@"%@",str_minute]; return format_time; } }else { int minutInt = (seconds%3600)%60; if (minutInt != 0) { NSString *format_time = [NSString stringWithFormat:@"%@时%d",str_hour,[str_minute intValue]+1]; return format_time; }else{ NSString *format_time = [NSString stringWithFormat:@"%@时%@",str_hour,str_minute]; return format_time; } } }else if(seconds == 0) { return @"0"; } else{ return @"1"; } }
6.获取文件的大小(下载课程视频获取大小时用到)
//返回文件大小的方法
+(NSString *)getFileSizeString:(CGFloat)size
{
if (size>1024*1024*1024){
return [NSString stringWithFormat:@"%.1fG",size/1024/1024/1024];//大于1G,则转化成G单位的字符串
}
else if(size<1024*1024*1024&&size>=1024*1024)//大于1M,则转化成M单位的字符串
{
return [NSString stringWithFormat:@"%.1fM",size/1024/1024];
}
else if(size>=1024&&size<1024*1024) //不到1M,但是超过了1KB,则转化成KB单位
{
return [NSString stringWithFormat:@"%.1fK",size/1024];
}
else//剩下的都是小于1K的,则转化成B单位
{
return [NSString stringWithFormat:@"%.1fB",size];
}
}
7.计算时间差(事前保存的时间与当前时间差 用于课程更新时间的显示)
+(NSString *)compareCurrentTime:(NSDate*)compareDate { NSTimeInterval timeInterval = [compareDate timeIntervalSinceNow]; timeInterval = -timeInterval; long temp = 0; NSString *result; NSLog(@"timeInterval:%f ",timeInterval); if (timeInterval < 60) { result = [NSString stringWithFormat:@"刚刚"]; } else if((temp = timeInterval/60) <60){ result = [NSString stringWithFormat:@"%ld分前",temp]; } else if((temp = temp/60) <24){ result = [NSString stringWithFormat:@"%ld小前",temp]; } else if((temp = temp/24) <30){ result = [NSString stringWithFormat:@"%ld天前",temp]; } else if((temp = temp/30) <12){ result = [NSString stringWithFormat:@"%ld月前",temp]; } else{ temp = temp/12; result = [NSString stringWithFormat:@"%ld年前",temp]; } return result; }
8.字符串转进制 (给后台提交数据时有用到)
//字符串转16进制 +(NSString *)unicoStr:(NSString *)text { NSString *hexstr = @""; for (int i=0;i< [text length];i++) { hexstr = [hexstr stringByAppendingFormat:@"%@",[NSString stringWithFormat:@"0x%1X ",[text characterAtIndex:i]]]; } return hexstr; } //字符串转8进制 +(NSString *)eightUnicoStr:(NSString *)text { NSString *hexstr = @""; // int slen = strlen([text UTF8String]); for (int i = 0; i < text.length; i++) { //fffffff0 去除前面六个F & 0xFF hexstr = [hexstr stringByAppendingFormat:@"%@",[NSString stringWithFormat:@"0x%X ",[text UTF8String][i] & 0xFF ]]; } return hexstr; }
8.字符串中的以另一种颜色标出个别文字,例如:已学:5分钟
//字符串文字显示两种颜色 +(NSMutableAttributedString *)stringTwoColor:(NSString *)allStr changeColorStr:(NSString *)changeColorStr { NSMutableAttributedString *inteMutStr = [[NSMutableAttributedString alloc] initWithString:allStr]; NSRange orangeRange = NSMakeRange([[inteMutStr string] rangeOfString:changeColorStr].location, [[inteMutStr string] rangeOfString:changeColorStr].length); [inteMutStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:orangeRange]; return inteMutStr; } 调用的例子: UILabel *myLable = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 30)]; [self.view addSubview:myLable]; NSString *numStr = @"4"; NSString *inteStr = [NSString stringWithFormat:@"已学:%@分",numStr]; [myLable setAttributedText:[self stringTwoColor:inteStr changeColorStr:numStr]]; 运行程序查看lable的文字,其中数字4会成红色