Swift:如何比较包含字母的字符串版本

米凯尔·贝尔哈森(Mickael Belhassen)

在我的应用程序中,我必须比较包含数字和字母的字符串版本,例如:

当我比较我有这个扩展,工作greaterThanlessThan

extension String {

    // Modified from the DragonCherry extension - https://github.com/DragonCherry/VersionCompare
    private func compare(toVersion targetVersion: String) -> ComparisonResult {
        let versionDelimiter = "."
        var result: ComparisonResult = .orderedSame
        var versionComponents = components(separatedBy: versionDelimiter)
        var targetComponents = targetVersion.components(separatedBy: versionDelimiter)

        while versionComponents.count < targetComponents.count {
            versionComponents.append("0")
        }

        while targetComponents.count < versionComponents.count {
            targetComponents.append("0")
        }

        for (version, target) in zip(versionComponents, targetComponents) {
            result = version.compare(target, options: .numeric)
            if result != .orderedSame {
                break
            }
        }

        return result
    }

    func isVersion(equalTo targetVersion: String) -> Bool { return compare(toVersion: targetVersion) == .orderedSame }

    func isVersion(greaterThan targetVersion: String) -> Bool { return compare(toVersion: targetVersion) == .orderedDescending }

    func isVersion(greaterThanOrEqualTo targetVersion: String) -> Bool { return compare(toVersion: targetVersion) != .orderedAscending }

    func isVersion(lessThan targetVersion: String) -> Bool { return compare(toVersion: targetVersion) == .orderedAscending }

    func isVersion(lessThanOrEqualTo targetVersion: String) -> Bool { return compare(toVersion: targetVersion) != .orderedDescending }

    static func ===(lhs: String, rhs: String) -> Bool { lhs.compare(toVersion: rhs) == .orderedSame }

    static func <(lhs: String, rhs: String) -> Bool { lhs.compare(toVersion: rhs) == .orderedAscending }

    static func <=(lhs: String, rhs: String) -> Bool { lhs.compare(toVersion: rhs) != .orderedDescending }

    static func >(lhs: String, rhs: String) -> Bool { lhs.compare(toVersion: rhs) == .orderedDescending }

    static func >=(lhs: String, rhs: String) -> Bool { lhs.compare(toVersion: rhs) != .orderedAscending }

}

当字符串还包含字母时,扩展名无法正常工作。其实我想比较一下:

0.21.16.AC04D0 === 0.21.16 // FALSE

预期:

0.21.16.AC04D0 === 0.21.16 // TRUE

对我来说0.21.16.AC04D0 === 0.21.16相同的版本

贾瓦德·阿里

用以下几行替换代码中的这两行

   var versionComponents = components(separatedBy: versionDelimiter)
                           .compactMap(Double.init)
                           .map { String($0) }

   var targetComponents = targetVersion.components(separatedBy: versionDelimiter)
                          .compactMap(Double.init)
                          .map { String($0) }

如果您需要完整的代码进行测试

extension String {

    // Modified from the DragonCherry extension - https://github.com/DragonCherry/VersionCompare
    private func compare(toVersion targetVersion: String) -> ComparisonResult {
        let versionDelimiter = "."
        var result: ComparisonResult = .orderedSame
        var versionComponents = components(separatedBy: versionDelimiter).compactMap(Double.init).map { String($0) }
        var targetComponents = targetVersion.components(separatedBy: versionDelimiter).compactMap(Double.init).map { String($0) }




        while versionComponents.count < targetComponents.count {

            versionComponents.append("0")
        }

        while targetComponents.count < versionComponents.count {
            targetComponents.append("0")
        }

        for (version, target) in zip(versionComponents, targetComponents) {
            result = version.compare(target, options: .numeric)
            if result != .orderedSame {
                break
            }
        }

        return result
    }

    func isVersion(equalTo targetVersion: String) -> Bool { return compare(toVersion: targetVersion) == .orderedSame }

    func isVersion(greaterThan targetVersion: String) -> Bool { return compare(toVersion: targetVersion) == .orderedDescending }

    func isVersion(greaterThanOrEqualTo targetVersion: String) -> Bool { return compare(toVersion: targetVersion) != .orderedAscending }

    func isVersion(lessThan targetVersion: String) -> Bool { return compare(toVersion: targetVersion) == .orderedAscending }

    func isVersion(lessThanOrEqualTo targetVersion: String) -> Bool { return compare(toVersion: targetVersion) != .orderedDescending }

    static func ===(lhs: String, rhs: String) -> Bool { lhs.compare(toVersion: rhs) == .orderedSame }

    static func <(lhs: String, rhs: String) -> Bool { lhs.compare(toVersion: rhs) == .orderedAscending }

    static func <=(lhs: String, rhs: String) -> Bool { lhs.compare(toVersion: rhs) != .orderedDescending }

    static func >(lhs: String, rhs: String) -> Bool { lhs.compare(toVersion: rhs) == .orderedDescending }

    static func >=(lhs: String, rhs: String) -> Bool { lhs.compare(toVersion: rhs) != .orderedAscending }

}

let val = "0.21.16.AC04D0" === "0.21.16"
print(val)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何比较字符串字母(javascript)

如何在C中仅通过字母比较包含数字的字符串

如何比较包含字符串的对象?

比较版本(字符串)

如何在Swift中检查字符串是否包含字母?

如何比较2个包含相同字符的字符串

如何检查字符串是否包含字母

Ruby如何比较语义版本字符串?

如何检查字符串是否包含字母字符或字母字符和数字?

按字母顺序比较字符串

PHP字符串与特殊字母的比较

如何在Powershell中比较包含问号(?)的字符串

如何比较JAVA中包含整数的字符串

如何比较列表中包含数字的字符串?

Python:比较2个字符串,看看它们是否包含相同的字母

Google Apps Script 搜索功能需要比较包含数字和字母的字符串

检查字符串在Swift中是否至少包含upperCase字母,数字或特殊字符?

如何检查是否字符串包含字母表中所有的字母?

如何检查字符串是否包含字母表中的任何字母?

Ruby:如何将包含连续字母组的字符串拆分为这些字母组?

如何比较字符串?

与具有相同字符或字母的字符串进行比较

将字符串中的字符与字母进行比较?

如何确定字符串在Swift中仅包含数字?

如何在Go中检查字符串是否仅包含字母字符?

如何检查字符串是否包含某些字母/字符

如何确定字符串是否包含非字母数字字符?

如何检查字符串是否包含空格/字母数字/等字符?

如何使用preg_match仅允许包含字母和数字字符的字符串?