美文网首页iOS猿媛圈
Swift- 字符串删除

Swift- 字符串删除

作者: FlyElephant | 来源:发表于2016-12-27 09:51 被阅读16次

    题目:输入两个字符串,从第一个字符串中删除在第二个中出现过的所偶字符串。例如从第一个字符串"We are students."中删除第二个字符中“aeiou”中出现过的字符得到的结果是"W r stdnts".
    核心代码:
    <pre><code>`
    func compareDelete(origin:String,delte:String) -> String {
    if origin.characters.count == 0 {
    return ""
    }

        var dict:[String:Int] = [:]
        for i in 0..<delte.characters.count {
            let char:String = delte[i]
            if dict[char] == nil {
                dict[char] = 1
            }
        }
        var result:String = ""
        for i in 0..<origin.characters.count  {
            let char:String = origin[i]
            if dict[char] == nil {
                result += char
            }
        }
        
        return result
    }`</code></pre>
    

    测试代码:
    <pre><code>var result:String = hashSearch.compareDelete(origin: "We are students.", delte: "aeiou") print("FlyElephatn-对比删除之后的字符串---\(result)")</code></pre>

    相关文章

      网友评论

        本文标题:Swift- 字符串删除

        本文链接:https://www.haomeiwen.com/subject/wavsvttx.html