This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
func reverse() -> String { | |
let result = self.reduce("") { (reversed, newChar) -> String in | |
return "\(newChar)" + reversed | |
} | |
return result | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
func capitalizingFirstLetter() -> String { | |
return prefix(1).capitalized + dropFirst() | |
} | |
mutating func capitalizeFirstLetter() { | |
self = self.capitalizingFirstLetter() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let hello = "Hello" | |
let thomas = "Thomas" | |
let hellothomas = [hello, thomas] | |
let combined = hello + thomas | |
let combined2 = "\(hello)\(thomas)" | |
let combined3 = hellothomas.joined() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let str = "Sunday, Monday, Happy Days" | |
print(str.lowercased()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let lines = str.components(separatedBy: "\n") | |
extension String { | |
var lines: [String] { | |
return self.components(separatedBy: "\n") | |
} | |
} |
OlderNewer