0

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


   }
    }

Hi! I need to an efficient way to find if a string contains any character from another string. I'm trying to see if my patientView string contains a character from donorView1, 2, 3 etc. Is there a way to find if my self.patientView.text string contains a character from my self.donorView1.text string? Thank you!

  • 6
    Not strictly related to your question, but have you considered putting all that duplicate code into a separate function and encapsulating those different pieces of information into an object / struct of some kind? Looking at all that duplicate code is making my eyes bleed. – Parthian Shot Mar 01 '16 at 19:00
  • have you tried this question? http://stackoverflow.com/questions/24034043/how-do-i-check-if-a-string-contains-another-string-in-swift – MSU_Bulldog Mar 01 '16 at 19:15
  • I have, but I'm using self.patientView.text so I'm not sure if it can read that string, because from what I read, it reads whatever text I input rather than a specific wrapped string. – Stressed High School Student Mar 01 '16 at 20:29
  • I think it would be really helpful to the people trying to help you if you provided some examples of a successful match and an example of two strings that don't match. I answered what I think you are trying to do, given that you have comma separated values in the strings. I don't think you are actually trying to find matching characters, but rather matching values. If I'm wrong, my answer won't be correct, but it illustrates the importance of being clear in your questions. – wottle Mar 01 '16 at 21:01

2 Answers2

1

It appears you want to find common sets of characters in two strings with a delimeter. For example A33, B14, C18, DQ5, DR3, DP1, and A24, B13, C6, DQ3, DR3, DP2, would return true because they share DR3.

However, A33, B14, C18, DQ5, DR3, DP1, and A66, B8, C8, DQ7, DR11, DP2, would return false because the set does not contain matches.

If that is the case, try the following (I cleaned up your Rendomize code to take out all the repetition:

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!))

}
wottle
  • 13,095
  • 4
  • 27
  • 68
0

This seems tailor-made for a Set:

let patient = "aabcdefgcad"
let donor = "123"
let patientSet = Set(patient.characters)
let donorSet = Set(donor.characters)

// isDisjointWith() is true if they don't have any characters in common
donorSet.isDisjointWith(patientSet) // => true
  • So what I'm doing is this: let patient = self.patientView.text let donor = self.donorView1.text let patientSet = Set(patient!.characters) let donorSet = Set(donor!.characters) // isDisjointWith() is true if they don't have any characters in common donorSet.isDisjointWith(patientSet) // => true } } but Xcode is telling me that "Result of call to 'isDisjointWith' is unused." How can I combat this issue? – Stressed High School Student Mar 01 '16 at 20:27
  • You need to use the result of that call. Save it in a variable or use it in an `if` statement. –  Mar 01 '16 at 20:37