Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, December 9, 2016

What is the best way to determine if a string contains a character from a set in Swift

What is the best way to determine if a string contains a character from a set in Swift


I need to determine if a string contains any of the characters from a custom set that I have defined.

I see from this post that you can use rangeOfString to determine if a string contains another string. This, of course, also works for characters if you test each character one at a time.

I'm wondering what the best way to do this is.

Answer by Cesare for What is the best way to determine if a string contains a character from a set in Swift


You could do it this way:

var string = "Hello, World!"    if string.rangeOfString("W") != nil {       println("exists")  } else {       println("doesn't exist")  }    // alternative: not case sensitive  if string.lowercaseString.rangeOfString("w") != nil {       println("exists")  } else {       println("doesn't exist")  }  

Answer by Martin R for What is the best way to determine if a string contains a character from a set in Swift


You could create a NSCharacterSet containing the set of your custom characters:

let charset = NSCharacterSet(charactersInString: "aw")  

and then test the membership against this character set:

if str.lowercaseString.rangeOfCharacterFromSet(charset, options: nil, range: nil) != nil {      println("yes")  }  

Answer by rintaro for What is the best way to determine if a string contains a character from a set in Swift


From Swift 1.2 you can do that using Set

var str = "Hello, World!"  let charset: Set = ["e", "n"]    charset.isSubsetOf(str)     // `true` if `str` contains all characters in `charset`  charset.isDisjointWith(str) // `true` if `str` does not contains any characters in `charset`  charset.intersect(str)      // set of characters both `str` and `charset` contains.  

Answer by Chris Hatton for What is the best way to determine if a string contains a character from a set in Swift


func isAnyCharacter( from charSetString: String, containedIn string: String ) -> Bool  {      return Set( charSetString.characters ).isDisjointWith( Set( string.characters) ) == false  }  

Answer by fs_tigre for What is the best way to determine if a string contains a character from a set in Swift


Swift 3:

let myString = "Some Words"    if (myString.range(of: "Some") != nil){      print("myString contains the word `Some`")  }else{      print("Word does not contain `Some`")  }  

Answer by AndyDunn for What is the best way to determine if a string contains a character from a set in Swift


Using Swift 3 to determine if your string contains characters from a specific NSCharacterSet:

    let letters = CharacterSet.alphanumerics      let string = "my-string_"      if (string.trimmingCharacters(in: letters) != "") {          print("Invalid characters in string.")      }      else {          print("Only letters and numbers.")      }  

Answer by t4ncr3d3 for What is the best way to determine if a string contains a character from a set in Swift


swift3

var str = "Hello, playground"  let res = str.characters.contains { ["x", "z"].contains( $0 ) }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.