【去除NSString 字符串中的空格换行符】

@interface NSString (DeletWhiteSpace)

// 返回一个去掉前后空格的字符串或者下划线,如果自己是一个nil 返回@“”

- (NSString *)trimmingWhiteSpace;

- (NSString *)aTrimmingWhiteSpaceInString;

@end

#import "NSString+DeletWhiteSpace.h"

@implementation NSString (DeletWhiteSpace)

- (NSString *)trimmingWhiteSpace{

    if (self) {

         return [selfstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]];

    }

    return nil;

}

- (NSString *)aTrimmingWhiteSpaceInString{

    NSArray *stringArray = [selfcomponentsSeparatedByCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]];

    if (stringArray) {

        NSMutableString *string = [NSMutableStringstring];

        for (NSString *str in stringArray) {

            if ([str trimmingWhiteSpace]) {

                [string appendString:[str trimmingWhiteSpace]];

            }

        }

        return string;

    }

    return  nil;

}

@end

  

上一篇:JSON: 介绍、应用


下一篇:[Educational Codeforces Round 16]D. Two Arithmetic Progressions