忽略开头或结尾的空格比较字符串

3

我是iOS开发的新手,正在寻找一种解决方案来比较两个字符串,忽略开头或结尾的空格。例如,"Hello" == "Hello " 应该返回true。

我已经搜索了Swift中的解决方案,但没有找到任何有用的东西。谢谢。


1
考虑修剪字符串。请参见:https://dev59.com/OG025IYBdhLWcg3w3J1H - Jack
1
这是一个指向Swift字符串参考的链接:http://www.learnswiftonline.com/reference-guides/string-reference-guide-for-swift/。在该页面中搜索“trim”。 - Blake Schwendiman
4个回答

5

我建议你使用这个 Swift 代码从字符串中修剪空白:

stringToTrim.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

这是最佳解决方案 - 空格仅从字符串的开头和结尾修剪,而不是从中间修剪。 "Roger Rabbit" 变成了 "Roger Rabbit"。 - Adam Eberbach

2
 NSString *string1 = @" Hello";
 //remove(trim) whitespaces
 string1 = [string1 stringByReplacingOccurrencesOfString:@" " withString:@""];

 NSString *string2 = @"Hello ";
 //remove(trim) whitespaces
 string2 = [string1 stringByReplacingOccurrencesOfString:@" " withString:@""]

 // compare strings without whitespaces
 if ([string1 isEuqalToString:string2]) {
 }

所以,如果你想直接使用它 -
if ([[yourString1 stringByReplacingOccurrencesOfString:@" " withString:@""] isEuqalToString:[yourString2 stringByReplacingOccurrencesOfString:@" " withString:@""]]) {
// Strings are compared without whitespaces.
}

上面的代码将删除字符串中的所有空格,如果您只想删除前导和尾随空格,则已经有一些帖子可供使用。您可以按照以下Stack Overflow帖子中提到的方式创建一个字符串类别 - 如何从NSString的右端删除空格?
@implementation NSString (TrimmingAdditions)

- (NSString *)stringByTrimmingLeadingCharactersInSet:(NSCharacterSet *)characterSet {
    NSUInteger location = 0;
    NSUInteger length = [self length];
    unichar charBuffer[length];    
    [self getCharacters:charBuffer];

    for (location; location < length; location++) {
        if (![characterSet characterIsMember:charBuffer[location]]) {
            break;
        }
    }

    return [self substringWithRange:NSMakeRange(location, length - location)];
}

- (NSString *)stringByTrimmingTrailingCharactersInSet:(NSCharacterSet *)characterSet {
    NSUInteger location = 0;
    NSUInteger length = [self length];
    unichar charBuffer[length];    
    [self getCharacters:charBuffer];

    for (length; length > 0; length--) {
        if (![characterSet characterIsMember:charBuffer[length - 1]]) {
            break;
        }
    }

    return [self substringWithRange:NSMakeRange(location, length - location)];
}

@end

现在,一旦您拥有可用的方法,就可以在字符串上调用这些方法来修剪前导和尾随空格,例如 -

 // trim leading chars
 yourString1 = [yourString1 stringByTrimmingLeadingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
// trim trainling chars
yourString1 = [yourString1 stringByTrimmingTrailingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

// trim leading chars
 yourString2 = [yourString2 stringByTrimmingLeadingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
// trim trainling chars
yourString2 = [yourString2 stringByTrimmingTrailingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

// compare strings
if([yourString1 isEqualToString: yourString2]) {

}

4
这会移除字符串中间的空格,你应该使用 stringByTrimmingCharactersInSet 只移除首尾空格。同时...这甚至不是 Swift 代码... - Jack
1
虽然这是被接受的答案,但它不是Swift。如果你在这里...并且正在寻找Swift...请继续阅读! - EricWasTaken

1

针对 Swift 3.0+

在比较之前使用.trimmingCharacters(in: .whitespaces).trimmingCharacters(in: .whitespacesAndNewlines)


-1
在 Swift 4 中,可以在任何 String 类型的变量上使用它。
extension String {
    func trimWhiteSpaces() -> String {
        let whiteSpaceSet = NSCharacterSet.whitespaces
        return self.trimmingCharacters(in: whiteSpaceSet)
    }
}

然后像这样调用它

yourString.trimWhiteSpaces()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接