Skip to content

Instantly share code, notes, and snippets.

@jweinst1
Created July 19, 2015 07:12
Show Gist options
  • Select an option

  • Save jweinst1/cce347f5edcdd7d76d90 to your computer and use it in GitHub Desktop.

Select an option

Save jweinst1/cce347f5edcdd7d76d90 to your computer and use it in GitHub Desktop.
For every a character appears in a string, it creates a slice of the string and appends it to an array
// returns a dictionary that holds the indexes for every character in a string
func StringIndexer(str:String) ->Dictionary<Int, Character> {
var strlist = Array(str); var strdict = [Int:Character](); var indexer = 0
for letter in strlist {
strdict.updateValue(letter, forKey: indexer)
indexer += 1
}
return strdict
}
// returns an array containing the indexes where a character appears in a string
func FindIndexCharacter(str:String, char:Character) ->Array<Int> {
let strdict = StringIndexer(str); var indexer = 0; var indexes = [Int]()
while indexer < count(str)-1 {
if strdict[indexer] == char {
indexes.append(indexer)
}
indexer += 1
}
return indexes
}
// Returns a slice of an array that contains characters
func StringSlicer(list:Array<Character>, n1:Int, n2:Int) ->Array<Character> {
var slicer = n1; var slicedlist = [Character]()
while slicer <= n2 {
slicedlist.append(list[slicer])
slicer += 1
}
return slicedlist
}
// creates an array of slices of a string everytme a character occurs
func SliceAtChracter(str:String, char:Character) ->[String] {
var indexes = FindIndexCharacter(str, char); var slices = [String](); var strlist = Array(str)
var startslice = 0; var endslice = 1
while endslice <= count(indexes)-1 {
slices.append(String(StringSlicer(strlist, indexes[startslice], indexes[endslice])))
startslice += 1; endslice += 1
}
return slices
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment