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

时间:2014-07-07 16:56:28

标签: ios string nsstring swift spaces

我是iOS开发的新手,我正在寻找一种解决方案来比较两个String,忽略开头或结尾处的空格。例如," Hello" =="您好"应该回归真实。

我已经找到了解决方案,但我在Swift中找不到任何东西。感谢

4 个答案:

答案 0 :(得分:4)

我建议你先用这个Swift代码修剪字符串中的空格:

stringToTrim.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

答案 1 :(得分:3)

 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.
}

上面将删除字符串的所有空格,如果你只想删除前导空格和尾随空格,那么有几个帖子已经可用,你可以创建一个字符串类别,如下面的堆栈溢出帖子所述 - {{3 }}

@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]) {

}

答案 2 :(得分:0)

对于Swift 3.0 +

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

答案 3 :(得分:-1)

在Swift 4中
在任何 String 类型变量上使用它。

extension String {
    func trimWhiteSpaces() -> String {
        let whiteSpaceSet = NSCharacterSet.whitespaces
        return self.trimmingCharacters(in: whiteSpaceSet)
    }
}

然后这样称呼

yourString.trimWhiteSpaces()