【问题标题】:Comparing String against array of String and counting the matches in Swift将字符串与字符串数组进行比较并计算 Swift 中的匹配项
【发布时间】:2018-08-01 01:20:37
【问题描述】:

我正在尝试将字符串 (userInput) 与字符串数组 (groupA) 进行比较,以检查 groupA 中有多少项存在于 userInput 中。

var groupA = ["game of thrones", "star wars", "star trek" ]
var userInput = "My name is oliver i love game of thrones, star wars and star trek."
var count = 0

func checking() -> Int {
    for item in groupA {
        // alternative: not case sensitive
        if userInput.lowercased().range(of:item) != nil {
            count + 1
        }
    }

    return count
}

func Printer() {
     print(count)
}

【问题讨论】:

  • 澄清一下,上面的代码不行吗?

标签: arrays swift string-matching


【解决方案1】:

您的代码设计得不是很好,因为您使用了很多全局变量,但只需进行一些小的更改即可:

var groupA = ["game of thrones", "star wars", "star trek" ]
var userInput = "My name is oliver i love game of thrones, star wars and star trek."

var count = 0
func checking() -> Int {
    for item in groupA {

        // alternative: not case sensitive
        if userInput.lowercased().range(of:item) != nil {
            count += 1  //Make this `count += 1`
        }
    }
    return count
}
func printer() {
    print(count)
}

//Remember to call `checking()` and `printer()`
checking()
printer()

还请注意,所有函数的命名都应以小写字母开头,因此Printer() 应为printer()

请考虑以下代码:

import UIKit

var groupA = ["game of thrones", "star wars", "star trek" ]
var userInput = "My name is oliver i love game of thrones, star wars and star trek."

//The `checking` function has been rewritten as `countOccurerences(ofStringArray:inString)`, 
//and now takes parameters and returns a value.
func countOccurrences(ofStringArray stringArray: [String],  inString string: String) -> Int {
    var result = 0
    for item in stringArray {

        // alternative: not case sensitive
        if string.lowercased().range(of:item) != nil {
            result += 1
        }
    }
    return result
}

//And the `printer()` function now takes parameter as well.
func printer(_ count: Int) {
    print("count = \(count)")
}

//Here is the code to use those 2 functions after refactoring
let count = countOccurrences(ofStringArray: groupA, inString: userInput)
printer(count)

【讨论】:

  • 一项改进是只调用一次lowercased。更好的是,使用range(of:options;).caseInsensitive(可能还有.diacriticInsensitive)。
  • 是的。我没有尝试完全重写 OP 的代码,只是提供了一些关于如何改进它的指针。
  • @rmaddy 你有没有对.caseInsensitive 字符串比较进行基准测试,看看它有多慢?我的直觉是事先将字符串转换为小写,然后使用常规字符串比较会更快,但还没有实际测试过。
【解决方案2】:

要使上述代码正常工作,您只需将第 18 行中的 count + 1 更改为 count += 1 我已经在下面发布了完整的代码。

import Cocoa
import Foundation

var groupA = ["game of thrones", "star wars", "star trek" ]
var userInput = "My name is oliver i love game of thrones, star wars and star trek."


    var count = 0
    func checking() -> Int {


        for item in groupA {


            // alternative: not case sensitive
            if userInput.lowercased().range(of:item) != nil {
                count += 1
            }


        }

        return count
    }



    func Printer() {
        print(count)
    }

    checking()
    Printer()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多