如何在 swift 3.0 中用 * 替换 @ 之前的电子邮件 ID 文本
How to replace Email id text before @ with * in swift 3.0
你好,我还没有找到完美的解决方案来解决如何用 * like
替换@之前的电子邮件 ID 文本
假设我的电子邮件 ID 是 XXXXXXX@gmail.com
那么我想要的输出是 *******@gmail.com
我这样试试
let delimiter = "@"
let newstr = "Himmoradiya@gmail.com"
var token = newstr.components(separatedBy: delimiter)
let newQuery = (token[0] as NSString).replacingCharacters(
in: NSMakeRange(0,token[0].characters.count), with: "*")
接受任何建议。
谢谢。
使用init(repeating:count:)
let newstr = "Himmoradiya@gmail.com"
var token = newstr.components(separatedBy: "@")
let newQuery = String(repeating: "*", count: token[0].characters.count) //check array length before using index
print(newQuery + "@" + token[1] ) // *******@gmail.com
你可以这样做,
let delimiter = "@"
let newstr = "Himmoradiya@gmail.com"
let arr = newstr.components(separatedBy: delimiter)
let resultStr = "xxxxxx" + "@" + arr[1]
print(resultStr)
使用这个功能。
func getStarred(_ email : String ) -> String
{
var didFoundATRO = false
var tempString = ""
for char in email.characters
{
if char == "@"
{
didFoundATRO = true
}
if !didFoundATRO
{
tempString += "*"
}
else
{
tempString.append(char)
}
}
return tempString
}
写这段代码:
let aString = "This is my string"
let newString = aString.replacingOccurrences(of: "@", with: "*")
let delimiter = "@"
var newstr = "Himmoradiya@gmail.com"
let arr = newstr.components(separatedBy: delimiter)
let strMail = arr.first!
let strReplaceStr = String(repeating: "*", count: strMail.characters.count)
newstr = newstr.replacingOccurrences(of: strMail, with: strReplaceStr)
和输出 (newstr
) 将是:***********@gmail.com
我创建了一个字符串扩展,例如...
extension String {
var hideEmailPrefix: String {
if self.contains("@") {
var part = self.components(separatedBy: "@")
let newText = String(repeating: "*", count: part[0].count)
return newText + "@" + part[1]
}
return self
}
}
然后在我的 ViewController:
override func viewDidLoad() {
super.viewDidLoad()
let email = "abc@gmail.com"
print(email.hideEmailPrefix) // ***@gmail.com
}
var maskEmail: String {
let email = self
let components = email.components(separatedBy: "@")
var maskEmail = ""
if let first = components.first {
maskEmail = String(first.enumerated().map { index, char in
return [0, 1, first.count - 1, first.count - 2].contains(index) ?
char : "*"
})
}
if let last = components.last {
maskEmail = maskEmail + "@" + last
}
return maskEmail
}
var maskPhoneNumber: String {
return String(self.enumerated().map { index, char in
return [0, 3, self.count - 1, self.count - 2].contains(index) ?
char : "*"
})
}
你好,我还没有找到完美的解决方案来解决如何用 * like
替换@之前的电子邮件 ID 文本假设我的电子邮件 ID 是 XXXXXXX@gmail.com
那么我想要的输出是 *******@gmail.com
我这样试试
let delimiter = "@"
let newstr = "Himmoradiya@gmail.com"
var token = newstr.components(separatedBy: delimiter)
let newQuery = (token[0] as NSString).replacingCharacters(
in: NSMakeRange(0,token[0].characters.count), with: "*")
接受任何建议。
谢谢。
使用init(repeating:count:)
let newstr = "Himmoradiya@gmail.com"
var token = newstr.components(separatedBy: "@")
let newQuery = String(repeating: "*", count: token[0].characters.count) //check array length before using index
print(newQuery + "@" + token[1] ) // *******@gmail.com
你可以这样做,
let delimiter = "@"
let newstr = "Himmoradiya@gmail.com"
let arr = newstr.components(separatedBy: delimiter)
let resultStr = "xxxxxx" + "@" + arr[1]
print(resultStr)
使用这个功能。
func getStarred(_ email : String ) -> String
{
var didFoundATRO = false
var tempString = ""
for char in email.characters
{
if char == "@"
{
didFoundATRO = true
}
if !didFoundATRO
{
tempString += "*"
}
else
{
tempString.append(char)
}
}
return tempString
}
写这段代码:
let aString = "This is my string"
let newString = aString.replacingOccurrences(of: "@", with: "*")
let delimiter = "@"
var newstr = "Himmoradiya@gmail.com"
let arr = newstr.components(separatedBy: delimiter)
let strMail = arr.first!
let strReplaceStr = String(repeating: "*", count: strMail.characters.count)
newstr = newstr.replacingOccurrences(of: strMail, with: strReplaceStr)
和输出 (newstr
) 将是:***********@gmail.com
我创建了一个字符串扩展,例如...
extension String {
var hideEmailPrefix: String {
if self.contains("@") {
var part = self.components(separatedBy: "@")
let newText = String(repeating: "*", count: part[0].count)
return newText + "@" + part[1]
}
return self
}
}
然后在我的 ViewController:
override func viewDidLoad() {
super.viewDidLoad()
let email = "abc@gmail.com"
print(email.hideEmailPrefix) // ***@gmail.com
}
var maskEmail: String {
let email = self
let components = email.components(separatedBy: "@")
var maskEmail = ""
if let first = components.first {
maskEmail = String(first.enumerated().map { index, char in
return [0, 1, first.count - 1, first.count - 2].contains(index) ?
char : "*"
})
}
if let last = components.last {
maskEmail = maskEmail + "@" + last
}
return maskEmail
}
var maskPhoneNumber: String {
return String(self.enumerated().map { index, char in
return [0, 3, self.count - 1, self.count - 2].contains(index) ?
char : "*"
})
}