Skip to content

Instantly share code, notes, and snippets.

View tkausch's full-sized avatar

Thomas Kausch tkausch

View GitHub Profile
@tkausch
tkausch / ReverseStringExtension.swift
Last active September 6, 2020 14:36
Swift Problem Solving
extension String {
func reverse() -> String {
let result = self.reduce("") { (reversed, newChar) -> String in
return "\(newChar)" + reversed
}
return result
}
}
@tkausch
tkausch / ROT13.swift
Last active September 18, 2020 15:18
How to calculate the ROT13 of a string
// ROT13 is a simple algorithm that shifts letters in a string forward 13 places.
struct ROT13 {
// create a dictionary that will store our character mapping
private static var key = [Character: Character]()
// create arrays of all uppercase and lowercase letters
private static let uppercase = Array("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
private static let lowercase = Array("abcdefghijklmnopqrstuvwxyz")
@tkausch
tkausch / CapitalizeFirstStringLetter.swift
Last active September 18, 2020 15:51
How to capitalize the first letter of a string
extension String {
func capitalizingFirstLetter() -> String {
return prefix(1).capitalized + dropFirst()
}
mutating func capitalizeFirstLetter() {
self = self.capitalizingFirstLetter()
}
}
@tkausch
tkausch / CapitalizeWordsInStrings.swift
Created September 18, 2020 15:58
How to capitalize words in a string using capitalized
// Swift offers several ways of adjusting the letter case of a string,
// but if you're looking for title case – that is, Text Where The First Letter Of Each String Is Capitalized -
// then you need to use the capitalized property, like this:
let str = "apple, banana, drink's fever"
print(str.capitalized)
@tkausch
tkausch / ContainsAnyWord.swift
Last active September 18, 2020 16:41
How to check whether a string contains any words from an array
// Remember you can check wether a string contains a single word
let sentence = "I'was eating pommes with chees and an Apple for desert."
print(sentence.contains("Apple"))
// ... and we can also check if an array contains a word
let words = ["Apple", "Banana", "Orange"]
print(words.contains("Apple"))
// We can combine together do any of the words fulfill where clause?
print(words.contains(where: sentence.contains))
@tkausch
tkausch / ConcatenateStrings
Created September 18, 2020 16:48
How to concatenate strings to make one joined string
let hello = "Hello"
let thomas = "Thomas"
let hellothomas = [hello, thomas]
let combined = hello + thomas
let combined2 = "\(hello)\(thomas)"
let combined3 = hellothomas.joined()
@tkausch
tkausch / ParseSentence.swift
Created September 18, 2020 18:27
How to parse a sentence and distinguish word types
let options = NSLinguisticTagger.Options.omitWhitespace.rawValue | NSLinguisticTagger.Options.joinNames.rawValue
let tagger = NSLinguisticTagger(tagSchemes: NSLinguisticTagger.availableTagSchemes(forLanguage: "en"), options: Int(options))
let inputString = "This is a very long test for you to try"
tagger.string = inputString
let range = NSRange(location: 0, length: inputString.utf16.count)
tagger.enumerateTags(in: range, scheme: .nameTypeOrLexicalClass, options: NSLinguisticTagger.Options(rawValue: options)) { tag, tokenRange, sentenceRange, stop in
guard let range = Range(tokenRange, in: inputString) else { return }
let token = inputString[range]
@tkausch
tkausch / ToLowerCaseLetters
Created October 23, 2020 14:47
How to convert a string to lowercase letters
let str = "Sunday, Monday, Happy Days"
print(str.lowercased())
@tkausch
tkausch / DetectUrlInString.swift
Created October 23, 2020 14:52
How to detect a URL in a String using NSDataDetector
let input = "My hidden URL https://www.hackingwithswift.com to be detected in a very long string."
let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
let matches = detector.matches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count))
for match in matches {
guard let range = Range(match.range, in: input) else { continue }
let url = input[range]
print(url)
}
@tkausch
tkausch / LinesOfStringAsArray.swift
Created October 23, 2020 14:55
How to get the lines in a string as an array
let lines = str.components(separatedBy: "\n")
extension String {
var lines: [String] {
return self.components(separatedBy: "\n")
}
}