首页 > 代码库 > NSString 过滤首尾空白、空行,把多换行转为单换行
NSString 过滤首尾空白、空行,把多换行转为单换行
- (NSString *)filterBlankAndBlankLines:(NSString *)str
{
NSMutableString *Mstr = [NSMutableStringstring];
NSArray *arr = [strcomponentsSeparatedByString:@"\n"];
for (int i = 0; i < arr.count; i++) {
NSString *tempStr = (NSString *)arr[i];
[tempStr stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]]; //去除掉首尾的空白字符和换行字符
[tempStr stringByReplacingOccurrencesOfString:@"\r"withString:@""];
[tempStr stringByReplacingOccurrencesOfString:@"\n"withString:@""];
if (tempStr.length !=0) {
[Mstr appendString:arr[i]];
if (i < [arr count] -1) {
[Mstr appendString:@"\n"];
}
}
}
return Mstr;
}