6

I have a string that contains a comma. However, I would like to replace the comma by a point. This must be possible unfortunately I can not get it done. I'm not just getting back the .5. The comma is away only everything that stonged for it unfortunately too.

let cijfers = "38,5"
let start = cijfers.startIndex;
let end = cijfers.index(cijfers.startIndex, offsetBy: 3);
let result = cijfers.replacingCharacters(in: start..<end, with: ".")
print(result)
André
  • 77
  • 1
  • 1
  • 5

1 Answers1

9

You use cijfers.replacingOccurrences not on the correct way, for your purpose. Try this:

let str = "38,5"
let replaced = str.replacingOccurrences(of: ",", with: ".")
print(replaced)
Wouter
  • 809
  • 7
  • 19