修剪NSString结尾的空格

我需要从string的末尾删除空格。 我怎样才能做到这一点? 例如:如果string是"Hello "它必须变成"Hello"

使用-stringByTrimmingCharactersInSet:

 NSString *string = @" this text has spaces before and after "; NSString *trimmedString = [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]]; 

(这将修剪两端的空白字符)。

Swift 3

 let string = " this text has spaces before and after " let trimmedString = string.trimmingCharacters(in: .whitespacesAndNewlines) 

另一个解决scheme涉及创build可变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" 

干得好…

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

所以现在就调用它。 假设你有一个string,前面有空格,最后是空格,你只是想删除末尾的空格,你可以这样调用它:

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

resultString将在最后没有空格。

要从Swift中的string的开头和结尾删除空格:

Swift 3

 string.trimmingCharacters(in: .whitespacesAndNewlines) 

以前的Swift版本

 string.stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet())) 
 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 

还有很多其他的CharacterSets。 根据您的要求自己检查一下。

Swift版本

只修剪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)))) } 

修剪string两边的空格:

 var str: String = " Yolo " var trimmedStr: String = str.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) 

这将只删除您select的尾随字符。

 func trimRight(theString: String, charSet: NSCharacterSet) -> String { var newString = theString while String(newString.characters.last).rangeOfCharacterFromSet(charSet) != nil { newString = String(newString.characters.dropLast()) } return newString } 

在Swift中

从string的两端修剪空格和换行符:

 var url: String = "\n http://example.com/xyz.mp4 " var trimmedUrl: String = url.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) 

我想出了这个函数,它基本上和Alex在答复中的performance类似:

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

whitespaceCharacterSet本身除了空格本身也包含tab字符,这在我的情况下是不能出现的。 所以我想一个简单的比较就足够了。

一个简单的解决scheme,只修剪一端,而不是两端在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 

还有一个对称的工具,仅用于修改开头:

 @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 
 NSString* NSStringWithoutSpace(NSString* string) { return [string stringByReplacingOccurrencesOfString:@" " withString:@""]; }