【问题标题】:How to find occurences of a lettter in the string array in swift如何快速查找字符串数组中出现的字母
【发布时间】:2021-11-05 04:33:31
【问题描述】:

我是尝试实现此逻辑的初学者,任何人都可以建议该逻辑。

func findLetterOccurence(Letter: String){
    let array = ["Data", "program", "questions", "Helpful"]

    ///Logic to find the given letter occurences in string array

    print("\(Letter) occured in \(count) times")
}

预期输出:a 出现了 3 次

我试过如下:

var count = 0
for i in array {
   var newArray.append(i)
   count = components(separatedBy: newArray).count - 1
}

但我不明白 components(separatedBy:) 内部的逻辑到底是什么?我的意思是如果没有更高的功能,我们怎么能在这里实现逻辑。

【问题讨论】:

  • 你尝试了什么?
  • 我已经更新了先生的问题,请看一下
  • 对数组中的每个元素使用components(separatedBy:)

标签: ios arrays swift objective-c xcode


【解决方案1】:

试试这样的:

 func findLetterOccurence(letter: String) {
    var count = 0
    for word in array { count += word.filter{ String($0) == letter}.count }
    print("--> \(letter) occured in \(count) times")
}

如果要不区分大小写,则必须进行调整,如下所示:

func findLetterOccurence(letter: String) {
    var count = 0
    for word in array { count += word.filter{ String($0).lowercased() == letter.lowercased()}.count }
    print("--> \(letter) occured in \(count) times")
}

【讨论】:

  • 我也喜欢使用过滤器的想法
【解决方案2】:

添加此扩展以查找字符串中后者的出现

extension String {
    func numberOfOccurrencesOf(string: String) -> Int {
        return self.components(separatedBy:string).count - 1
    }
}

将它用于数组

        func findLetterOccurence(Letter: String){
        let array = ["Data", "program", "questions", "Helpful"]
        var number = 0
        for str in array{
            var l = Letter
//            uncomment it to use code for upper case and lower case both
//            l = str.lowercased()
            
            number = number + str.numberOfOccurrencesOf(string: l)
        }
        print("\(Letter) occured in \(number) times")
    }

【讨论】:

    【解决方案3】:

    几种方法。

        @discardableResult func findLetterOccurence(letter: String) -> Int {
        
        let array = ["Data", "program", "questions", "Helpful"]
        var count = 0
        // here we join the array into a single string. Then for each character we check if the lowercased version matches the string lowercased value. 
        array.joined().forEach({ if $0.lowercased() == letter.lowercased() { count += 1} } )
        
        print("\(letter) occured in \(count) times")
        
        return count
    }
    

    你也可以做一个敏感的比较,不用管外壳 通过说

    array.joined().forEach({ if String($0) == letter { count += 1} } )
    

    另一种方式是这样

    //here our argument is a character because maybe we just want to search for a single letter.
        @discardableResult func findLetterOccurence2(character: Character) -> Int {
        
            let array = ["Data", "program", "questions", "Helpful"]
       
        //again join the array into a single string. and reduce takes the `into` parameter and passes it into the closure as $0 in this case, and each element of the string gets passed in the second argument of the closure.
            let count = array.joined().reduce(into: 0) {
               $0 += $1.lowercased() == letter.lowercased() ? 1 : 0
            }
            print("\(letter) occured in \(count) times")
        
            return count
        }
    

    【讨论】:

      猜你喜欢
      • 2014-03-30
      • 2017-01-02
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多