如何查看字符串是否与另一个字符串中的任何字符匹配?

时间:2016-03-01 18:52:59

标签: ios swift

extension String {

func contains(find: String) -> Bool{
    return self.rangeOfString(find) != nil
}

}

@IBOutlet weak var patientView: UITextField!

@IBAction func patientFunction(sender: UIButton!) {

}
@IBOutlet weak var donorView1: UITextField!

@IBOutlet weak var donorView2: UITextField!

@IBOutlet weak var donorView3: UITextField!

@IBOutlet weak var donorView4: UITextField!

@IBOutlet weak var donorView5: UITextField!

@IBOutlet weak var donorView6: UITextField!

@IBOutlet weak var donorView7: UITextField!

@IBOutlet weak var donorView8: UITextField!

@IBOutlet weak var Randomize: UIButton!

@IBAction func Randomize(sender: UIButton!) {

    let quoteArray1 = [" A1,", " A2,", " A3,", " A11,", " A23, ", " A24,", " A25,", " A26,", " A29,", " A30,", " A31,", " A32, ", " A33,", " A34,", " A36, ", " A43,", " A66,", " A68, ", " A69,", " A74,", " A80,",  ]
    let quoteArray2 = [" B7,", " B8,", " B13,", " B14,", " B15, ", " B18,", " B27,", " B35,", " B37,", " B38,", " B39,", " B40,", " B41,", " B42,", " B44,", " B45,", " B46,", " B47,", " B48,", " B49,"," B50,", " B51,", " B52,", " B53,", " B54,", " B55,", " B56,", " B57,", " B58,", " B59,", " B67,"," B73,", " B78,", " B81,", " B82,", " B83," ]
    let quoteArray3 = [" C1,", " C2,", " C3,", " C4,", " C5, ", " C6,", " C7,", " C8,", " C12,", " C14,", " C15,", " C16,", " C17,", " C18," ]
    let quoteArray4 = [" DQ1,", " DQ2,", " DQ3,", " DQ4,", " DQ5,", " DQ6,", " DQ7,", " DQ8,", " DQ9,", ]
    let quoteArray5 = [" DR1,", " DR3,", " DR4,", " DR7,", " DR8,", " DR9,", " DR10,", " DR11,", " DR12,", " DR13,", " DR14,", " DR15,", " DR16,", ]
    let quoteArray6 = [" DP1,", " DP2,"]
    let randomRange1 = UInt32(quoteArray1.count)
    let randomRange2 = UInt32(quoteArray2.count)
    let randomRange3 = UInt32(quoteArray3.count)
    let randomRange4 = UInt32(quoteArray4.count)
    let randomRange5 = UInt32(quoteArray5.count)
    let randomRange6 = UInt32(quoteArray6.count)
    let texty1 = quoteArray4 [Int(arc4random_uniform(randomRange4))] + quoteArray5 [Int(arc4random_uniform(randomRange5))] + quoteArray6 [Int(arc4random_uniform(randomRange6))]
    let texty2 = quoteArray4 [Int(arc4random_uniform(randomRange4))] + quoteArray5 [Int(arc4random_uniform(randomRange5))] + quoteArray6 [Int(arc4random_uniform(randomRange6))]
    let texty3 = quoteArray4 [Int(arc4random_uniform(randomRange4))] + quoteArray5 [Int(arc4random_uniform(randomRange5))] + quoteArray6 [Int(arc4random_uniform(randomRange6))]
    let texty4 = quoteArray4 [Int(arc4random_uniform(randomRange4))] + quoteArray5 [Int(arc4random_uniform(randomRange5))] + quoteArray6 [Int(arc4random_uniform(randomRange6))]
    let texty5 = quoteArray4 [Int(arc4random_uniform(randomRange4))] + quoteArray5 [Int(arc4random_uniform(randomRange5))] + quoteArray6 [Int(arc4random_uniform(randomRange6))]
    let texty6 = quoteArray4 [Int(arc4random_uniform(randomRange4))] + quoteArray5 [Int(arc4random_uniform(randomRange5))] + quoteArray6 [Int(arc4random_uniform(randomRange6))]
    let texty7 = quoteArray4 [Int(arc4random_uniform(randomRange4))] + quoteArray5 [Int(arc4random_uniform(randomRange5))] + quoteArray6 [Int(arc4random_uniform(randomRange6))]
    let texty8 = quoteArray4 [Int(arc4random_uniform(randomRange4))] + quoteArray5 [Int(arc4random_uniform(randomRange5))] + quoteArray6 [Int(arc4random_uniform(randomRange6))]
    let texty9 = quoteArray4 [Int(arc4random_uniform(randomRange4))] + quoteArray5 [Int(arc4random_uniform(randomRange5))] + quoteArray6 [Int(arc4random_uniform(randomRange6))]
        self.patientView.text = quoteArray1 [Int(arc4random_uniform(randomRange1))] + quoteArray2 [Int(arc4random_uniform(randomRange2))] + quoteArray3 [Int(arc4random_uniform(randomRange3))] + texty1
        self.donorView1.text = quoteArray1 [Int(arc4random_uniform(randomRange1))] + quoteArray2 [Int(arc4random_uniform(randomRange2))] + quoteArray3 [Int(arc4random_uniform(randomRange3))] + texty2
        self.donorView2.text = quoteArray1 [Int(arc4random_uniform(randomRange1))] + quoteArray2 [Int(arc4random_uniform(randomRange2))] + quoteArray3 [Int(arc4random_uniform(randomRange3))] + texty3
        self.donorView3.text = quoteArray1 [Int(arc4random_uniform(randomRange1))] + quoteArray2 [Int(arc4random_uniform(randomRange2))] + quoteArray3 [Int(arc4random_uniform(randomRange3))] + texty4
        self.donorView4.text = quoteArray1 [Int(arc4random_uniform(randomRange1))] + quoteArray2 [Int(arc4random_uniform(randomRange2))] + quoteArray3 [Int(arc4random_uniform(randomRange3))] + texty5
        self.donorView5.text = quoteArray1 [Int(arc4random_uniform(randomRange1))] + quoteArray2 [Int(arc4random_uniform(randomRange2))] + quoteArray3 [Int(arc4random_uniform(randomRange3))] + texty6
        self.donorView6.text = quoteArray1 [Int(arc4random_uniform(randomRange1))] + quoteArray2 [Int(arc4random_uniform(randomRange2))] + quoteArray3 [Int(arc4random_uniform(randomRange3))] + texty7
        self.donorView7.text = quoteArray1 [Int(arc4random_uniform(randomRange1))] + quoteArray2 [Int(arc4random_uniform(randomRange2))] + quoteArray3 [Int(arc4random_uniform(randomRange3))] + texty8
        self.donorView8.text = quoteArray1 [Int(arc4random_uniform(randomRange1))] + quoteArray2 [Int(arc4random_uniform(randomRange2))] + quoteArray3 [Int(arc4random_uniform(randomRange3))] + texty9

    let pv = self.patientView.text
    let d1 = self.donorView1.text
    let d2 = self.donorView2.text
    let d3 = self.donorView3.text
    let d4 = self.donorView4.text
    let d5 = self.donorView5.text
    let d6 = self.donorView6.text
    let d7 = self.donorView7.text
    let d8 = self.donorView8.text

    print(pv!.contains(d1!)) // true
    print(pv!.contains(d2!)) // true
    print(pv!.contains(d3!)) // true
    print(pv!.contains(d4!)) // true
    print(pv!.contains(d5!)) // true
    print(pv!.contains(d6!)) // true
    print(pv!.contains(d7!)) // true
    print(pv!.contains(d8!)) // true


   }
    }

嗨!我需要一种有效的方法来查找字符串是否包含来自另一个字符串的任何字符。我正在尝试查看我的patientView字符串是否包含来自donorView1,2,3等的字符。有没有办法找到我的self.patientView.text字符串是否包含来自我的self.donorView1.text字符串的字符?谢谢!

2 个答案:

答案 0 :(得分:1)

您似乎希望在带有分隔符的两个字符串中找到常见的字符集。例如,A33, B14, C18, DQ5, DR3, DP1,A24, B13, C6, DQ3, DR3, DP2,会返回true,因为它们共享DR3

然而, A33, B14, C18, DQ5, DR3, DP1,A66, B8, C8, DQ7, DR11, DP2,将返回false,因为该集合不包含匹配项。

如果是这种情况,请尝试以下操作(我清理了您的重新随机代码以取消所有重复:

func commonElements( firstString: String, secondString: String )-> Bool
{
    let firstStringElements = firstString.componentsSeparatedByString(",")
    let secondStringElements = secondString.componentsSeparatedByString(",")

    for  firstStringElement: String in firstStringElements {
        for secondStringElement: String in secondStringElements {
            let string1 =  firstStringElement.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
            let string2 =  secondStringElement.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
            if string1.lowercaseString == string2.lowercaseString && string1 != ""
            {
                print( "Found match (%@) in strings %@ and %@", string1.lowercaseString, firstString, secondString)
                return true
            }
        }
    }

    return false
}

func randomEntryFromArray( theQuoteArray: [String]) -> String
{
    let randomRange = UInt32(theQuoteArray.count)

    return theQuoteArray [Int(arc4random_uniform(randomRange))]
}

func randomEntriesFromAllArrays() -> String
{
    let quoteArray1 = [" A1,", " A2,", " A3,", " A11,", " A23, ", " A24,", " A25,", " A26,", " A29,", " A30,", " A31,", " A32, ", " A33,", " A34,", " A36, ", " A43,", " A66,", " A68, ", " A69,", " A74,", " A80,",  ]
    let quoteArray2 = [" B7,", " B8,", " B13,", " B14,", " B15, ", " B18,", " B27,", " B35,", " B37,", " B38,", " B39,", " B40,", " B41,", " B42,", " B44,", " B45,", " B46,", " B47,", " B48,", " B49,"," B50,", " B51,", " B52,", " B53,", " B54,", " B55,", " B56,", " B57,", " B58,", " B59,", " B67,"," B73,", " B78,", " B81,", " B82,", " B83," ]
    let quoteArray3 = [" C1,", " C2,", " C3,", " C4,", " C5, ", " C6,", " C7,", " C8,", " C12,", " C14,", " C15,", " C16,", " C17,", " C18," ]
    let quoteArray4 = [" DQ1,", " DQ2,", " DQ3,", " DQ4,", " DQ5,", " DQ6,", " DQ7,", " DQ8,", " DQ9,", ]
    let quoteArray5 = [" DR1,", " DR3,", " DR4,", " DR7,", " DR8,", " DR9,", " DR10,", " DR11,", " DR12,", " DR13,", " DR14,", " DR15,", " DR16,", ]
    let quoteArray6 = [" DP1,", " DP2,"]


    let returnText = randomEntryFromArray(quoteArray1) + randomEntryFromArray(quoteArray2) + randomEntryFromArray(quoteArray3) + randomEntryFromArray(quoteArray4) + randomEntryFromArray(quoteArray5) + randomEntryFromArray(quoteArray6)

    return returnText
}

@IBAction func Randomize(sender: UIButton!) {


self.patientView.text = randomEntriesFromAllArrays()
self.donorView1.text = randomEntriesFromAllArrays()
self.donorView2.text = randomEntriesFromAllArrays()
self.donorView3.text = randomEntriesFromAllArrays()
self.donorView4.text = randomEntriesFromAllArrays()
self.donorView5.text = randomEntriesFromAllArrays()
self.donorView6.text = randomEntriesFromAllArrays()
self.donorView7.text = randomEntriesFromAllArrays()
self.donorView8.text = randomEntriesFromAllArrays()

print(commonElements( self.patientView.text!, secondString: self.donorView1.text!))
print(commonElements( self.patientView.text!, secondString: self.donorView2.text!))
print(commonElements( self.patientView.text!, secondString: self.donorView3.text!))
print(commonElements( self.patientView.text!, secondString: self.donorView4.text!))
print(commonElements( self.patientView.text!, secondString: self.donorView5.text!))
print(commonElements( self.patientView.text!, secondString: self.donorView6.text!))
print(commonElements( self.patientView.text!, secondString: self.donorView7.text!))
print(commonElements( self.patientView.text!, secondString: self.donorView8.text!))

}

答案 1 :(得分:0)

这似乎是针对

量身定制的