我有一个包含逗号的字符串。但是,我想用一点代替逗号。不幸的是,这一定是可能的,我无法完成它。我不仅仅是回到.5。不幸的是,逗号也只消失了所有的东西。
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)
答案 0 :(得分:3)
出于您的目的,您使用cijfers.replacingOccurrences
的方式不正确。
试试这个:
let str = "38,5"
let replaced = str.replacingOccurrences(of: ",", with: ".")
print(replaced)