I have a two strings. I want to check if the letters of the 2nd string are a part of the first or not in swift? How to do it?
Asked
Active
Viewed 380 times
-7
-
Do you wish to check that one string contains another entirely? For example, "ABC DEF" contains substring "C D" – Alexander Dec 28 '16 at 16:19
-
2What exactly are you looking for? Should the second string be a *substring* of the first, or can the letters of the second string be contained in the first string *in any order?* Please clarify the question, perhaps with concrete examples of what you are trying to achieve. – Martin R Dec 28 '16 at 16:29
2 Answers
3
Simplest way:
let string = "Some string"
if string.range(of: "Some") != nil {
//do what you need
}
And if you need case insensitive check:
if string.lowercased().range(of: "some") != nil {
//do what you need
}
Artem Novichkov
- 2,301
- 2
- 22
- 34
0
let str1 = "Aman"
let str2 = "an"
str1.contains(str2) //returns true.
Hope this helps.
Aman Gupta
- 957
- 1
- 8
- 18
-
-
you want to check that if second string is sub-string of first string..right?? – Aman Gupta Dec 28 '16 at 16:28
-