Trim spaces from end of a NSString
我需要从字符串的末尾删除空格。 我怎样才能做到这一点?
示例:如果string为
使用-stringByTrimmingCharactersInSet:
1 2 3 | NSString *string = @" this text has spaces before and after"; NSString *trimmedString = [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; |
(这将修剪两端的空白字符)。
斯威夫特3
1 2 | let string =" this text has spaces before and after" let trimmedString = string.trimmingCharacters(in: .whitespacesAndNewlines) |
另一个解决方案是创建可变字符串:
1 2 3 4 5 6 7 | //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" |
干得好...
1 2 3 4 5 6 7 8 9 10 11 12 13 | - (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)]; } |
所以现在就叫它吧。假设你有一个字符串,前面有空格,末尾有空格,你只想删除末尾的空格,你可以像这样调用它:
1 2 3 | NSString *oneTwoThree = @" TestString "; NSString *resultString; resultString = [self removeEndSpaceFrom:oneTwoThree]; |
要从Swift中的字符串的开头和结尾删除空格:
斯威夫特3
1 | string.trimmingCharacters(in: .whitespacesAndNewlines) |
以前的Swift版本
1 | string.stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet())) |
1 2 3 4 5 6 7 | 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。根据您的要求自行检查。
这将仅删除您选择的尾随字符。
1 2 3 4 5 6 7 8 9 10 | 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版本
只修剪字符串末尾的空格:
1 2 3 4 5 6 7 8 9 | 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)))) } |
修剪字符串两边的空格:
1 2 | var str: String =" Yolo" var trimmedStr: String = str.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) |
要修剪所有尾随空格字符(我猜这实际上是你的意图),以下是一个非常简洁明了的方法:
1 | NSString *trimmedString = [string stringByReplacingOccurrencesOfString:@"\\s+$" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, string.length)]; |
一行,带有一些正则表达式。
在Objective-C中只修剪一端而不是两端的简单解决方案:
1 2 3 4 5 6 7 8 9 10 11 12 | @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 |
并且仅用于修剪开头的对称实用程序:
1 2 3 4 5 6 7 8 9 10 11 12 | @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 |
在斯威夫特
要从字符串的两边修剪空格和换行符:
1 2 3 | var url: String =" http://example.com/xyz.mp4 " var trimmedUrl: String = url.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) |
这里描述了解决方案:如何从NSString的右端删除空格?
将以下类别添加到NSString:
1 2 3 4 5 6 7 8 9 10 11 12 13 | - (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]]; } |
你这样使用它:
1 | [yourNSString stringByTrimmingTrailingWhitespaceAndNewlineCharacters] |
我想出了这个函数,它基本上与Alex的答案中的行为类似:
1 2 3 4 5 | -(NSString*)trimLastSpace:(NSString*)str{ int i = str.length - 1; for (; i >= 0 && [str characterAtIndex:i] == ' '; i--); return [str substringToIndex:i + 1]; } |
let string ="Test Trimmed String"
对于删除空格和新行使用以下代码: -
让str_trimmed = yourString.trimmingCharacters(in:.whitespacesAndNewlines)
仅用于删除代码中使用字符串的空格: -
让str_trimmed = yourString.trimmingCharacters(in:.whitespaces)
1 2 3 4 | NSString* NSStringWithoutSpace(NSString* string) { return [string stringByReplacingOccurrencesOfString:@"" withString:@""]; } |