一、根据字符串的宽度计算字符串的高度
-(CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize string:(NSString *)string{
NSDictionary *attrs = @{NSFontAttributeName:font};
return [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
二、字符串截取、匹配、分隔
//1.截取字符串
NSString *string =@"123456d890";
NSString *str1 = [string substringToIndex:5];//截取掉下标5之前的字符串
NSLog(@"截取的值为:%@",str1);
NSString *str2 = [string substringFromIndex:3];//截取掉下标3之后的字符串
NSLog(@"截取的值为:%@",str2);
//2.匹配字符串
NSString *string =@"sd是sfsfsAdfsdf";
NSRange range = [string rangeOfString:@"Ad"];//匹配得到的下标
NSLog(@"rang:%@",NSStringFromRange(range));
string = [string substringWithRange:range];//截取范围内的字符串
NSLog(@"截取的值为:%@",string);
//3.分隔字符串
NSString *string =@"sdfsfsfsAdfsdf";
NSArray *array = [string componentsSeparatedByString:@"A"]; //从字符A中分隔成2个元素的数组
NSLog(@"array:%@",array); //结果是adfsfsfs和dfsdf
三、字符串替换
NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@" " withString:@""]; 去掉空格
NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@" " withString:@"/"];替换字符
网友评论