我需要从字符串末尾删除空格。我怎样才能做到这一点?示例:如果为string,"Hello "
则必须变为"Hello"
我需要从字符串末尾删除空格。我怎样才能做到这一点?示例:如果为string,"Hello "
则必须变为"Hello"
Answers:
从这里的答案中获取: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
}
whitespaceAndNewlineCharacterSet
。
干得好...
- (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)];
}
所以现在就叫它。假设您有一个字符串,该字符串的前面有空格,结尾有空格,而您只想删除结尾的空格,则可以这样称呼它:
NSString *oneTwoThree = @" TestString ";
NSString *resultString;
resultString = [self removeEndSpaceFrom:oneTwoThree];
resultString
末尾将没有空格。
要在Swift中仅从字符串的开头和结尾删除空格,请执行以下操作:
string.trimmingCharacters(in: .whitespacesAndNewlines)
string.stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet()))
stringByTrimmigCharactersInSet:
-“返回通过从接收者的两端删除给定字符集中包含的字符而构成的新字符串。” developer.apple.com/reference/foundation/nsstring/…–
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
还有许多其他的字符集。根据您的要求自己检查。
迅捷版
仅修剪字符串末尾的空格:
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))))
}
修剪字符串两侧的空格:
var str: String = " Yolo "
var trimmedStr: String = str.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
在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
要修剪所有结尾的空白字符(我想这实际上是您的意图),以下是一种非常简洁的方法。
斯威夫特5:
let trimmedString = string.replacingOccurrences(of: "\\s+$", with: "", options: .regularExpression)
目标C:
NSString *trimmedString = [string stringByReplacingOccurrencesOfString:@"\\s+$" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, string.length)];
一行,带有一些正则表达式。
解决方案在这里描述:如何从NSString的右端删除空格?
将以下类别添加到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]];
}
您可以这样使用它:
[yourNSString stringByTrimmingTrailingWhitespaceAndNewlineCharacters]
NSString* NSStringWithoutSpace(NSString* string)
{
return [string stringByReplacingOccurrencesOfString:@" " withString:@""];
}