Trim spaces from end of a NSString

前端 未结 14 1069
既然无缘
既然无缘 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:36

    Swift version

    Only trims spaces at the end of the String:

    private func removingSpacesAtTheEndOfAString(var str: String) -> String {
        var i: Int = countElements(str) - 1, j: Int = i
    
        while(i >= 0 && str[advance(str.startIndex, i)] == " ") {
            --i
        }
    
        return str.substringWithRange(Range<String.Index>(start: str.startIndex, end: advance(str.endIndex, -(j - i))))
    }
    

    Trims spaces on both sides of the String:

    var str: String = " Yolo "
    var trimmedStr: String = str.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
    
    0 讨论(0)
  • 2020-11-27 09:36

    To trim all trailing whitespace characters (I'm guessing that is actually your intent), the following is a pretty clean & concise way to do it.

    Swift 5:

    let trimmedString = string.replacingOccurrences(of: "\\s+$", with: "", options: .regularExpression)
    

    Objective-C:

    NSString *trimmedString = [string stringByReplacingOccurrencesOfString:@"\\s+$" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, string.length)];
    

    One line, with a dash of regex.

    0 讨论(0)
  • let string = " Test Trimmed String "

    For Removing white Space and New line use below code :-

    let str_trimmed = yourString.trimmingCharacters(in: .whitespacesAndNewlines)

    For Removing only Spaces from string use below code :-

    let str_trimmed = yourString.trimmingCharacters(in: .whitespaces)

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

    Taken from this answer here: https://stackoverflow.com/a/5691567/251012

    - (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
    }
    
    0 讨论(0)
  • 2020-11-27 09:40

    Another solution involves creating mutable string:

    //make mutable string
    NSMutableString *stringToTrim = [@" i needz trim " mutableCopy];
    
    //pass it by reference to CFStringTrimSpace
    CFStringTrimWhiteSpace((__bridge CFMutableStringRef) stringToTrim);
    
    //stringToTrim is now "i needz trim"
    
    0 讨论(0)
  • 2020-11-27 09:41

    I came up with this function, which basically behaves similarly to one in the answer by Alex:

    -(NSString*)trimLastSpace:(NSString*)str{
        int i = str.length - 1;
        for (; i >= 0 && [str characterAtIndex:i] == ' '; i--);
        return [str substringToIndex:i + 1];
    }
    

    whitespaceCharacterSet besides space itself includes also tab character, which in my case could not appear. So i guess a plain comparison could suffice.

    0 讨论(0)
提交回复
热议问题