0

I need to find the position of a character in a string and to return an Int for further calculation.

let idx = cycleOrder.characters.indexOf(charInput)

This gives me the index, but how do I get this into an Int?

I tried:

intIdx = Int(idx)

but that doesn't work.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
brandenwagner
  • 138
  • 1
  • 5
  • 20

1 Answers1

1

check this code for find index of given character as Int

 func findIndexOfCharacter(str :String, findElement: Character) -> Int? {
  for (index, value) in Array(str.characters).enumerate() {
   if value == findElement {
    return index
  }
}
return nil
}

// Console:
 findIndexOfCharacter("hello", findElement: "e") // 1
Narendra G
  • 491
  • 4
  • 9