Last active
August 29, 2015 14:23
-
-
Save epohs/3f610c57c8121c2244c5 to your computer and use it in GitHub Desktop.
Find all occurances of a character in a string using Swift
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 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