Replace characters in Swift String?

Viewed 9963

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)
1 Answers

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)
Related