NSString 筛选和最后一个空白、空行,多换行成一个新行
- (NSString *)filterBlankAndBlankLines:(NSString *)str
{
NSMutableString *Mstr = [NSMutableString string];
NSArray *arr = [str componentsSeparatedByString:@"\n"];
for (int i = 0; i < arr.count; i++) {
NSString *tempStr = (NSString *)arr[i];
[tempStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //去除掉首尾的空白字符和换行字符
[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;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。