gpt4 book ai didi

swift - 如何屏蔽字符串以仅显示最后 3 个字符?

转载 作者:行者123 更新时间:2023-11-28 09:42:24 24 4
gpt4 key购买 nike

我只是尝试如下屏蔽 String,但经过一些搜索和研究后我没有找到我想要的东西。

字符串 a = "0123456789"掩码 = "xxxxxxx789"

我在这个问题 http://stackoverflow.com/questions/41224637/masking-first-and-last-name-string-with 中修改了解决方案,但它只是更改了与模式不匹配的字符串。我不知道如何更改模式以符合我的意思。

最佳答案

您可以使用收集方法 suffix(_ maxLength: Int) 获取字符串的最后 3 个字符,并重复“x”填充字符串的其他部分:


编辑/更新

Swift 4 或更高版本

extension StringProtocol {
var masked: String {
return String(repeating: "•", count: Swift.max(0, count-3)) + suffix(3)
}
}

let string = "0123456789"
print(string.masked) // "•••••••789\n"

关于swift - 如何屏蔽字符串以仅显示最后 3 个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44068702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com