Skip to content

Instantly share code, notes, and snippets.

@epohs
Last active August 29, 2015 14:23
Show Gist options
  • Save epohs/3f610c57c8121c2244c5 to your computer and use it in GitHub Desktop.
Save epohs/3f610c57c8121c2244c5 to your computer and use it in GitHub Desktop.
Find all occurances of a character in a string using Swift
let search_str = "l"
let string = "Hello World"
let needle:Character = search_str[search_str.startIndex]
let upper:String = String(needle).uppercaseString
var char_locations:[String] = ["-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"]
for (index, character) in enumerate(Array(string)) {
if (character == needle) {
char_locations.insert(upper, atIndex:index)
}
}
println("\(upper) was found at location \(char_locations) in '\(string)'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment