Trim spaces from end of a NSString

前端 未结 14 1071
既然无缘
既然无缘 2020-11-27 09:26

I need to remove spaces from the end of a string. How can I do that? Example: if string is \"Hello \" it must become \"Hello\"

相关标签:
14条回答
  • 2020-11-27 09:49

    The solution is described here: How to remove whitespace from right end of NSString?

    Add the following categories to NSString:

    - (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]];
    }
    

    And you use it as such:

    [yourNSString stringByTrimmingTrailingWhitespaceAndNewlineCharacters]
    
    0 讨论(0)
  • 2020-11-27 09:50
    NSString* NSStringWithoutSpace(NSString* string)
    {
        return [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
    
    0 讨论(0)
  • 2020-11-27 09:51

    A simple solution to only trim one end instead of both ends in Objective-C:

    @implementation NSString (category)
    
    /// trims the characters at the end
    - (NSString *)stringByTrimmingSuffixCharactersInSet:(NSCharacterSet *)characterSet {
        NSUInteger i = self.length;
        while (i > 0 && [characterSet characterIsMember:[self characterAtIndex:i - 1]]) {
            i--;
        }
        return [self substringToIndex:i];
    }
    
    @end
    

    And a symmetrical utility for trimming the beginning only:

    @implementation NSString (category)
    
    /// trims the characters at the beginning
    - (NSString *)stringByTrimmingPrefixCharactersInSet:(NSCharacterSet *)characterSet {
        NSUInteger i = 0;
        while (i < self.length && [characterSet characterIsMember:[self characterAtIndex:i]]) {
            i++;
        }
        return [self substringFromIndex:i];
    }
    
    @end
    
    0 讨论(0)
  • 2020-11-27 09:56

    Here you go...

    - (NSString *)removeEndSpaceFrom:(NSString *)strtoremove{
        NSUInteger location = 0;
        unichar charBuffer[[strtoremove length]];
        [strtoremove getCharacters:charBuffer];
        int i = 0;
        for(i = [strtoremove length]; i >0; i--) {
            NSCharacterSet* charSet = [NSCharacterSet whitespaceCharacterSet];
            if(![charSet characterIsMember:charBuffer[i - 1]]) {
                break;
            }
        }
        return [strtoremove substringWithRange:NSMakeRange(location, i  - location)];
    }
    

    So now just call it. Supposing you have a string that has spaces on the front and spaces on the end and you just want to remove the spaces on the end, you can call it like this:

    NSString *oneTwoThree = @"  TestString   ";
    NSString *resultString;
    resultString = [self removeEndSpaceFrom:oneTwoThree];
    

    resultString will then have no spaces at the end.

    0 讨论(0)
  • 2020-11-27 09:57
    NSString *trimmedString = [string stringByTrimmingCharactersInSet:
                                  [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    //for remove whitespace and new line character
    
    NSString *trimmedString = [string stringByTrimmingCharactersInSet:
                                  [NSCharacterSet punctuationCharacterSet]]; 
    //for remove characters in punctuation category
    

    There are many other CharacterSets. Check it yourself as per your requirement.

    0 讨论(0)
  • 2020-11-27 09:59

    In Swift

    To trim space & newline from both side of the String:

    var url: String = "\n   http://example.com/xyz.mp4   "
    var trimmedUrl: String = url.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
    
    0 讨论(0)
提交回复
热议问题