alexpaul / Character-and-CharacterSet

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Character

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.

isLowercase

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
*/

isCurrencySymbol

let amount = "$100"
let firstChar = amount.first ?? " "
if firstChar.isCurrencySymbol {
  print("valid currency")
} else {
  print("not a valid currency")
}

Many other Character checks available

char.isASCII

char.isCased

char.asciiValue

char.isLetter

char.isMathSymbol

char.isWholeNumber

char.isWhitespace

char.isNumber

char.isLowercase

char.isNewline

char.isPunctuation

Resources

  1. Apple documentation - Character

CharacterSet

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.

Built-in CharacterSets

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
*/

Building your own CharacterSet

Check if a String has vowels

let vowels = CharacterSet(charactersIn: "aeiou")
let str = "alex"
if let _ = str.rangeOfCharacter(from: vowels) {
  print(true)
} else {
  print(false)
}

Check for symbol in String

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
*/

Combining CharacterSets

isSubset

nameCharSet.isSubset(of: .alphanumerics) // true

isSuperset

nameCharSet.isSuperset(of: CharacterSet(charactersIn: "lex")) // true

isStrictSubset

CharacterSet(charactersIn: "lexi").isStrictSubset(of: nameCharSet) // false
CharacterSet(charactersIn: "lex").isStrictSubset(of: nameCharSet) // true

disjoint

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"

Useful use cases

Removing puctuation

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

Removing symbols

var currentStatus = "๐Ÿฅณ Everything is awesome ๐Ÿ”ฅ ๐ŸŽ†"
currentStatus = currentStatus.components(separatedBy: .symbols).joined()
print(currentStatus) // Everything is awesome

Validating data

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")
}

Challenges

Challenge 1 - First unique character in a String

LeetCode

Challenge 2 - Find common characters

LeetCode

Challenge 3 - Shortest distance to a Character

LeetCode

Challenge 4 - Consecutive characters

LeetCode

Challenge 5 - Pangram

HackerRank

Resouruces

  1. Apple documentation - CharacterSet
  2. Unicode Character Categories
  3. NSHipster - CharacterSet

About

License:MIT License