【问题标题】:Comparing String ignoring spaces at the beginning or ending比较字符串忽略开头或结尾的空格
【发布时间】:2014-08-28 05:53:48
【问题描述】:

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

我已经搜索了一个解决方案,但我在 Swift 中什么也没找到。谢谢

【问题讨论】:

标签: ios string nsstring swift spaces


【解决方案1】:

我建议您首先使用以下 Swift 代码修剪字符串中的空格:

stringToTrim.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

【讨论】:

  • 这是最好的解决方案 - 空格只从开头和结尾修剪,而不是从字符串的中间修剪。 “兔子罗杰”变成了“兔子罗杰”。
【解决方案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.
}

上面将删除字符串的所有空格,如果您只想删除前导和尾随空格,那么已经有几个帖子可用,您可以创建一个字符串类别,如以下堆栈溢出帖子中所述 - How to remove whitespace from right end of 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]) {

}

【讨论】:

  • 这将删除字符串中间的空格,您应该使用stringByTrimmingCharactersInSet 仅删除前导和尾随空格。另外...这甚至不在 Swift 中...
  • 虽然这是公认的答案,但它不是 Swift。如果您在这里……并且正在寻找 Swift……请继续阅读!
【解决方案3】:

对于 Swift 3.0+

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

【讨论】:

    【解决方案4】:

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

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

    然后这样称呼它

    yourString.trimWhiteSpaces()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多