I want get value before symbol @ or number in swift. I have email which is john@gmail.com. I want get john only. Another example is peter34@gmail.com. I want get peter only.
Asked
Active
Viewed 1,204 times
-2
Mohammad Yusuf
- 16,554
- 10
- 50
- 78
MAS. John
- 582
- 6
- 22
2 Answers
4
Use components(separatedBy:) passing it a CharacterSet composed of @ and the digits, and then use first to get the first part of the symbol:
let emails = ["john@gmail.com", "peter34@gmail.com"]
for email in emails {
if let name = email.components(separatedBy: CharacterSet(charactersIn: ("@0123456789"))).first {
print(name)
}
}
Output:
john peter
vacawama
- 150,663
- 30
- 266
- 294
-2
try this
let EMAIL= "peter34@gmail.com"
let EMAILARR= EMAIL.characters.split{$0 == "@"}.map(String.init)
EMAILARR[0] // to get peter34
EMAILARR[1] // to get gmail.com
Denny Sutedja
- 538
- 4
- 15