从字符串中按位置删除字符

耶稣·罗伊萨·埃雷拉

我想按位置从字符串中删除两个字符,表单示例我想从subCadenaTX4位置4和5中删除字符

let fechaNacRFC = "25/12/1997"
let subCadenaTX4 = fechaNacRFC!.replacingOccurrences(of: "/", with: "", options: NSString.CompareOptions.literal, range: nil)
print(subCadenaTX4) // It will be 25121997

我读到可以使用,removeAtIndex但是在这种情况下该怎么办?

我的预期输出: 251297

瓦瓦瓦玛

我只是使用prefixandsuffix构建一个新字符串dropFirst

let subCadenaTX4 = "25121997"

// Desire: drop characters at position 4 and 5
// use prefix to get the first 4 characters
// use suffix to get the last 2 characters
// call String() to covert the result back to String
let result = String(subCadenaTX4.prefix(4) + subCadenaTX4.suffix(2))

要么:

// Desire: drop characters at position 4 and 5
// use prefix to get the first 4 characters
// Use dropFirst(6) to pick string starting at position 6
// call String to convert result to String
let result = String(subCadenaTX4.prefix(4) + subCadenaTX4.dropFirst(6))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章