有时候,后台返回的字符串全部是小写,而我们需要大写展示,又或者后台返回的字符串全部是大写,我们需要小写展示,怎么办?其实很简单,看代码
//小写转大写
-(NSString *)stringToUpper:(NSString *)str{
for(NSInteger i =0 ; i < str.length ; i++){
if([str characterAtIndex:i] >='a' & [str characterAtIndex:i] <='z') {
char temp = [str characterAtIndex:i] -32;
NSRange range =NSMakeRange(i,1);
str = [str stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"%c",temp]];
}
}
return str;
}
//大写转小写
-(NSString *)stringToLower:(NSString *)str{
for(NSInteger i = 0 ; i < str.length ; i++) {
if([str characterAtIndex:i] >='A'& [str characterAtIndex:i] <='Z') {
char temp = [str characterAtIndex:i]+32;
NSRange range =NSMakeRange(i,1);
str = [strs tringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"%c",temp]];
}
}
return str;
}
直接复制拿走用
网友评论