How to Check If a String Contains Another String in Swift

How do I check if a string contains another string in Swift?

You can do exactly the same call with Swift:

Swift 4 & Swift 5

In Swift 4 String is a collection of Character values, it wasn't like this in Swift 2 and 3, so you can use this more concise code1:

let string = "hello Swift"
if string.contains("Swift") {
print("exists")
}

Swift 3.0+

var string = "hello Swift"

if string.range(of:"Swift") != nil {
print("exists")
}

// alternative: not case sensitive
if string.lowercased().range(of:"swift") != nil {
print("exists")
}

Older Swift

var string = "hello Swift"

if string.rangeOfString("Swift") != nil{
println("exists")
}

// alternative: not case sensitive
if string.lowercaseString.rangeOfString("swift") != nil {
println("exists")
}

I hope this is a helpful solution since some people, including me, encountered some strange problems by calling containsString().1

PS. Don't forget to import Foundation

Footnotes

  1. Just remember that using collection functions on Strings has some edge cases which can give you unexpected results, e. g. when dealing with emojis or other grapheme clusters like accented letters.

How to check if a string contains a substring within an array of strings in Swift?

First solution is for exactly matching the word or phrase in wordGroups using regex

var isMatch = false
for word in wordGroups {
let regex = "\\b\(word)\\b"
if string.range(of: regex, options: .regularExpression) != nil {
isMatch = true
break
}
}

As suggested in the comments the above loop can be replace with a shorter contains version

let isMatch = wordGroups.contains { 
string.range(of: "\\b\($0)\\b", options: .regularExpression) != nil
}

Second solution is for simply text if string contains the any of the strings in the array

let isMatch2 = wordGroups.contains(where: string.contains)

So for "A very nice beach" both returns true but for "Some very nice beaches" only the second one returns true

How to check if a string contains another string but a character can vary?

You can use a regular expression "He[a-z]{2}o World". This would require any two lowercase letters between e and o:

let sentence = "Hello World"
let pattern = "He[a-z]{2}o World"
if sentence.range(of: pattern, options: .regularExpression) != nil {
print(true)
}

How do I check if a string contains another string in Objective-C?

NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound) {
NSLog(@"string does not contain bla");
} else {
NSLog(@"string contains bla!");
}

The key is noticing that rangeOfString: returns an NSRange struct, and the documentation says that it returns the struct {NSNotFound, 0} if the "haystack" does not contain the "needle".


And if you're on iOS 8 or OS X Yosemite, you can now do: (*NOTE: This WILL crash your app if this code is called on an iOS7 device).

NSString *string = @"hello bla blah";
if ([string containsString:@"bla"]) {
NSLog(@"string contains bla!");
} else {
NSLog(@"string does not contain bla");
}

(This is also how it would work in Swift)

Swift – How to find out if a string contains several identical characters?

You can use filter(_:) on the string and count to get the number of dots:

let str = "3..14"

switch str.filter({ $0 == "." }).count {
case 0:
print("string has no dots")
case 1:
print("string has 1 dot")
default:
print("string has 2 or more dots")
}

Detect if string contains any element of a string array

In additional to answer of @Sh_Khan, if you want match some word from group:

let str:String = "house near the beach"
let wordGroups:[String] = ["beach","waterfront","with a water view","near ocean","close to water"]
let worlds = wordGroups.flatMap { $0.components(separatedBy: " ")}
let match = worlds.filter { str.range(of:$0) != nil }.count != 0

How can I check if a string already contains a character in Swift

You can use contains() method

let myString = "test"
if myString.contains("e"){
let index = myString.index(of: "e")
myString.remove(at: index!)
}

p.s you can get myString from textField.text



Related Topics



Leave a reply



Submit