Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode Swift - Is there an opposite to .contains?

Is there a way to determine if string does not contain something in an if statement? Basically the opposite of .contains .

Example of contains:

if someItem.contains(x) {
code
}

Example of what I am looking for:

if someItem.doesNotContain(x) {
code
}

Where a slolution for doesNotContain is what I am looking for.

Any help is greatly appreciated, thank you.

*FYI, I am new to Swift and to programming, thank you for your comments.

like image 970
CrazyOne Avatar asked Dec 29 '25 09:12

CrazyOne


1 Answers

No Apple doesn't provide any function like doesNotContain or something similar.

But if you don't want to add else part then you can simply use:

if !someItem.contains("hello") {
    //This means your someItem doesn't contain "hello" string.
}
like image 105
Dharmesh Kheni Avatar answered Jan 01 '26 01:01

Dharmesh Kheni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!