Himanshu Moradiya
Himanshu Moradiya

Reputation: 4815

How to replace Email id text before @ with * in swift 3.0

Hello I have not get perfect solution for how to replace Email Id text before @ with * like

suppose my email id is [email protected] then my output I want like *******@gmail.com

I try like this

    let delimiter = "@"
    let newstr = "[email protected]"
    var token = newstr.components(separatedBy: delimiter)

    let newQuery = (token[0] as NSString).replacingCharacters(
        in: NSMakeRange(0,token[0].characters.count), with: "*")

Any suggestion is accepted.

Thank you.

Upvotes: 0

Views: 2103

Answers (7)

Dharma
Dharma

Reputation: 3013

Use init(repeating:count:)

  let newstr = "[email protected]"
  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

Upvotes: 7

Ashi
Ashi

Reputation: 161

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 : "*"
    })
     }

Upvotes: 0

cs4alhaider
cs4alhaider

Reputation: 1446

I have created a string extension like...

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
   }
}

Then in my ViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    let email = "[email protected]"
    print(email.hideEmailPrefix) // ***@gmail.com
}

Upvotes: 0

Amal T S
Amal T S

Reputation: 3415

        let delimiter = "@"
        var newstr = "[email protected]"
        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)

and output (newstr) will be : ***********@gmail.com

Upvotes: 0

Raman Srivastava
Raman Srivastava

Reputation: 396

Write this code:

let aString = "This is my string"
let newString = aString.replacingOccurrences(of: "@", with: "*")

Upvotes: 1

Ketan Parmar
Ketan Parmar

Reputation: 27448

You can do like,

    let delimiter = "@"
    let newstr = "[email protected]"
    let arr = newstr.components(separatedBy: delimiter)

    let resultStr = "xxxxxx" + "@" + arr[1]

    print(resultStr)

Upvotes: 0

Sudipto Roy
Sudipto Roy

Reputation: 6795

Use this function .

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
    }

Upvotes: 0

Related Questions