Apple documentation: The Character type represents a character made up of one or more Unicode scalar values, grouped by a Unicode boundary algorithm. Generally, a > Character instance matches what the reader of a string will perceive as a single character. Strings are collections of Character instances, so the number of visible characters is generally the most natural way to count the length of a string.
for char in "Alex" {
if char.isLowercase {
print("\(char) is a lower letter")
}
}
/*
l is a lower letter
e is a lower letter
x is a lower letter
*/
let amount = "$100"
let firstChar = amount.first ?? " "
if firstChar.isCurrencySymbol {
print("valid currency")
} else {
print("not a valid currency")
}
char.isASCII
char.isCased
char.asciiValue
char.isLetter
char.isMathSymbol
char.isWholeNumber
char.isWhitespace
char.isNumber
char.isLowercase
char.isNewline
char.isPunctuation
Apple Documentation: A CharacterSet represents a set of Unicode-compliant characters. Foundation types use CharacterSet to group characters together for searching > operations, so that they can find any of a particular set of characters during a search.
for char in "Alex".unicodeScalars {
if CharacterSet.lowercaseLetters.contains(char) {
print("\(char) is a lower letter")
}
}
/*
l is a lower letter
e is a lower letter
x is a lower letter
*/
let vowels = CharacterSet(charactersIn: "aeiou")
let str = "alex"
if let _ = str.rangeOfCharacter(from: vowels) {
print(true)
} else {
print(false)
}
Here we are iteracting the elements of the "currentYear" String normally we would be getting each element as a "String.Index" or "Character" data type however here we need to inspect the unicodeScalar of our happyMood CharacterSet so we need to convert our "currentYear" String to unicodeScalars thereby we are interating throught unicode scalars
var happyMood = CharacterSet(charactersIn: "๐ฅณ๐ฅ๐")
happyMood.insert(charactersIn: "๐")
happyMood.insert(charactersIn: "๐")
let currentYear = "๐๐ข๐ญ๐คฌ๐ฑ๐ค๐คฎ"
for unicodeScalar in currentYear.unicodeScalars {
if happyMood.contains(unicodeScalar) {
print("\(unicodeScalar) happy")
} else {
print("\(unicodeScalar) sad")
}
}
/*
๐ happy
๐ข sad
๐ญ sad
๐คฌ sad
๐ฑ sad
๐ค sad
๐คฎ sad
*/
nameCharSet.isSubset(of: .alphanumerics) // true
nameCharSet.isSuperset(of: CharacterSet(charactersIn: "lex")) // true
CharacterSet(charactersIn: "lexi").isStrictSubset(of: nameCharSet) // false
CharacterSet(charactersIn: "lex").isStrictSubset(of: nameCharSet) // true
CharacterSet(charactersIn: "wix").isDisjoint(with: nameCharSet) // false because "x" exist in nameCharSet
CharacterSet(charactersIn: "swift").isDisjoint(with: nameCharSet) // true because all characters in "swift" are unique to "Alex"
var sentence = "Swift is awesome! When are you joining the fun?"
sentence = sentence.components(separatedBy: .punctuationCharacters).joined()
print(sentence) // Swift is awesome When are you joining the fun
var currentStatus = "๐ฅณ Everything is awesome ๐ฅ ๐"
currentStatus = currentStatus.components(separatedBy: .symbols).joined()
print(currentStatus) // Everything is awesome
var name = "Alex"
if CharacterSet(charactersIn: name).isSubset(of: CharacterSet.letters) {
print("name comprises of only letters") // name comprises of only letters
} else {
print("not a valid name")
}
var password = "alex1234"
if CharacterSet(charactersIn: password).isSubset(of: CharacterSet.alphanumerics) {
print("valid password created") // valid password created
} else {
print("not a valid password")
}