ios - Use of rangeOfCharacter in Swift 3.0 -
i attempting use rangeofcharacter create app, unable understand documentation:
func rangeofcharacter(from: characterset, options: string.compareoptions, range: range<string.index>?) -finds , returns range in string of first character given character set found in given range given options.
documentation link: https://developer.apple.com/documentation/swift/string#symbols
i working on exercise create function take in name , return name, minus consonants before first vowel. name should returned unchanged if there no consonants before first vowel.
below code have far:
func shortnamefromname(name: string) -> string { var shortname = name.lowercased() let vowels = "aeiou" let vowelrange = characterset(charactersin: vowels) rangeofcharacter(from: vowels, options: shortname, range: substring(from: shortname[0]))
any appreciated. apologies newbie mistakes.
i hate swift ranges. things better swift 4.
let name = "michael" var shortname = name.lowercased() let vowels = "aeiou" let vowelset = characterset(charactersin: vowels) let stringset = shortname if let range = stringset.rangeofcharacter(from: vowelset, options: string.compareoptions.caseinsensitive) { let startindex = range.lowerbound let substring = name.substring(from: range.lowerbound) print(substring) }
Comments
Post a Comment