Search code examples
objective-cnsstringtrimstripremoving-whitespace

How to remove whitespace from right end of NSString?


This removes white space from both ends of a string:

NSString *newString = [oldString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

How do I remove white space from just the right end of a string?

UPDATE: The code from the accepted answer is now part of SSToolkit. Yay!


Solution

  • Building on the answers by @Regexident & @Max, I came up with the following methods:

    @implementation NSString (SSToolkitAdditions)
    
    #pragma mark Trimming Methods
    
    - (NSString *)stringByTrimmingLeadingCharactersInSet:(NSCharacterSet *)characterSet {
        NSRange rangeOfFirstWantedCharacter = [self rangeOfCharacterFromSet:[characterSet invertedSet]];
        if (rangeOfFirstWantedCharacter.location == NSNotFound) {
            return @"";
        }
        return [self substringFromIndex:rangeOfFirstWantedCharacter.location];
    }
    
    - (NSString *)stringByTrimmingLeadingWhitespaceAndNewlineCharacters {
        return [self stringByTrimmingLeadingCharactersInSet:
                [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    }
    
    - (NSString *)stringByTrimmingTrailingCharactersInSet:(NSCharacterSet *)characterSet {
        NSRange rangeOfLastWantedCharacter = [self rangeOfCharacterFromSet:[characterSet invertedSet]
                                                                   options:NSBackwardsSearch];
        if (rangeOfLastWantedCharacter.location == NSNotFound) {
            return @"";
        }
        return [self substringToIndex:rangeOfLastWantedCharacter.location+1]; // non-inclusive
    }
    
    - (NSString *)stringByTrimmingTrailingWhitespaceAndNewlineCharacters {
        return [self stringByTrimmingTrailingCharactersInSet:
                [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    }
    
    @end
    

    And here are the GHUnit tests, which all pass of course:

    @interface StringCategoryTest : GHTestCase
    @end
    
    @implementation StringCategoryTest
    
    - (void)testStringByTrimmingLeadingCharactersInSet {
        NSCharacterSet *letterCharSet = [NSCharacterSet letterCharacterSet];
        GHAssertEqualObjects([@"zip90210zip" stringByTrimmingLeadingCharactersInSet:letterCharSet], @"90210zip", nil);
    }
    
    - (void)testStringByTrimmingLeadingWhitespaceAndNewlineCharacters {
        GHAssertEqualObjects([@"" stringByTrimmingLeadingWhitespaceAndNewlineCharacters], @"", nil);
        GHAssertEqualObjects([@"\n \n " stringByTrimmingLeadingWhitespaceAndNewlineCharacters], @"", nil);
        GHAssertEqualObjects([@"\n hello \n" stringByTrimmingLeadingWhitespaceAndNewlineCharacters], @"hello \n", nil);
    }
    
    - (void)testStringByTrimmingTrailingCharactersInSet {
        NSCharacterSet *letterCharSet = [NSCharacterSet letterCharacterSet];
        GHAssertEqualObjects([@"zip90210zip" stringByTrimmingTrailingCharactersInSet:letterCharSet], @"zip90210", nil);
    }
    
    - (void)testStringByTrimmingTrailingWhitespaceAndNewlineCharacters {
        GHAssertEqualObjects([@"" stringByTrimmingLeadingWhitespaceAndNewlineCharacters], @"", nil);
        GHAssertEqualObjects([@"\n \n " stringByTrimmingLeadingWhitespaceAndNewlineCharacters], @"", nil);
        GHAssertEqualObjects([@"\n hello \n" stringByTrimmingTrailingWhitespaceAndNewlineCharacters], @"\n hello", nil);
    }
    
    @end
    

    I submitted a GitHub pull request to SSToolkit with these methods added.