gpt4 book ai didi

swift - 用星号替换字符

转载 作者:行者123 更新时间:2023-11-28 15:00:46 26 4
gpt4 key购买 nike

我的 Playground 有这段代码。

我已经将用户 ID 和电子邮件扩展名分开了。但是,我想使用 * 更改电子邮件的第一个和最后一个字符之间的字符, 除外。

我正在尝试通过任何电子邮件地址输入使其足够动态。有什么想法吗?谢谢您的意见。

let email = "asdfg.hjkl@gmail.com"
let atSign = email.index(of: "@") ?? email.endIndex
let userID = email[..<atSign]
print(userID + email.suffix(from: atSign))

最佳答案

我只找到了一个迭代解决方案:

let email = "asdfg.hjkl@gmail.com"
let atSign = email.index(of: "@") ?? email.endIndex
let userID = email[..<atSign]
print(userID + email.suffix(from: atSign))


var lastLetterInx = email.index(before:atSign)

var inx = email.startIndex

var result = ""
while(true) {
if (inx >= lastLetterInx) {
result.append(String(email[lastLetterInx...]))
break;
}

if (inx > email.startIndex && email[inx] != ".") {
result.append("*")
} else {
result.append(email[inx])
}

inx = email.index(after:inx)
}

print (result)

关于swift - 用星号替换字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48944895/

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