Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In Objective-C the code to check for a substring in an NSString is:

NSString *string = @"hello Swift"; NSRange textRange =[string rangeOfString:@"Swift"]; if(textRange.location != NSNotFound) {     NSLog(@"exists"); } 

But how do I do this in Swift?

like image 924
Rajneesh071 Avatar asked Jun 04 '14 09:06

Rajneesh071


People also ask

How do you check if a string contains another string?

You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.

How do you check if a string contains a number in Swift?

Convert to NSString and you can use string. containsString(otherString) and just check if it contains a number.

How do you match a string in Swift?

In Swift, you can check for string and character equality with the "equal to" operator ( == ) and "not equal to" operator ( != ).

Is string in Swift?

A string is a series of characters, such as "Swift" , that forms a collection. Strings in Swift are Unicode correct and locale insensitive, and are designed to be efficient. The String type bridges with the Objective-C class NSString and offers interoperability with C functions that works with strings.


2 Answers

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.
like image 189
Jens Wirth Avatar answered Sep 24 '22 22:09

Jens Wirth


Extension way

Swift 4

extension String {     func contains(find: String) -> Bool{         return self.range(of: find) != nil     }     func containsIgnoringCase(find: String) -> Bool{         return self.range(of: find, options: .caseInsensitive) != nil     } }  var value = "Hello world"  print(value.contains("Hello")) // true print(value.contains("bo"))    // false  print(value.containsIgnoringCase(find: "hello"))    // true print(value.containsIgnoringCase(find: "Hello"))    // true print(value.containsIgnoringCase(find: "bo"))       // false 

Generally Swift 4 has contains method however it available from iOS 8.0+


Swift 3.1

You can write extension contains: and containsIgnoringCase for String

extension String {      func contains(_ find: String) -> Bool{      return self.range(of: find) != nil    }     func containsIgnoringCase(_ find: String) -> Bool{      return self.range(of: find, options: .caseInsensitive) != nil     }  } 

Older Swift version

extension String {      func contains(find: String) -> Bool{        return self.rangeOfString(find) != nil      }      func containsIgnoringCase(find: String) -> Bool{        return self.rangeOfString(find, options: NSStringCompareOptions.CaseInsensitiveSearch) != nil      } } 

Example:

var value = "Hello world"  print(value.contains("Hello")) // true print(value.contains("bo"))    // false  print(value.containsIgnoringCase("hello"))    // true print(value.containsIgnoringCase("Hello"))    // true print(value.containsIgnoringCase("bo"))       // false 
like image 36
Maxim Shoustin Avatar answered Sep 24 '22 22:09

Maxim Shoustin