替换Swift字符串中的字符?
我有一个包含逗号的字符串。但是,我想用一个点来代替逗号。这一定是可能的,可惜我做不到。我不仅仅是要回电话。逗号是唯一的,不幸的是,它也失去了一切替换Swift字符串中的字符?,swift,Swift,我有一个包含逗号的字符串。但是,我想用一个点来代替逗号。这一定是可能的,可惜我做不到。我不仅仅是要回电话。逗号是唯一的,不幸的是,它也失去了一切 let cijfers = "38,5" let start = cijfers.startIndex; let end = cijfers.index(cijfers.startIndex, offsetBy: 3); let result = cijfers.replacingCharacters(in: start..<end, with:
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)
让cijfers=“38,5”
让start=cijfers.startIndex;
设end=cijfers.index(cijfers.startIndex,offsetBy:3);
让result=cijfers.replacingCharacters(in:start..出于您的目的,您使用的cijfers.replacingOccurrences
方法不正确。
试试这个:
let str = "38,5"
let replaced = str.replacingOccurrences(of: ",", with: ".")
print(replaced)