2016-03-21 39 views
-1

我正在開發聊天應用程序。用戶需要使用大學郵件地址註冊。在MyPlayground中提取電子郵件域

我的應用程序將嚴重依賴於使用UnivID(大學ID)和'@'符號以及電子郵件域劃分用戶。例如,如果用戶使用以下任一電子郵件地址註冊:[email protected], [email protected], or [email protected] The UnivID = '@havard.edu'

我是新來的編碼,所以我希望不要侮辱你的智慧,如果這很容易實現。

乾杯

+1

你的問題兄弟是什麼? – iMuzahid

+0

嗨,對不起,我不清楚。我期待創建一個字符串='@ domain.edu',無論用戶是否註冊了諸如[email protected],[email protected]或[email protected]的電子郵件 –

回答

0

我假設你實際上想要在&符後面的所有東西。如果是這種情況,那麼這個代碼將起作用。

let example = "[email protected]" 
let univId = "@" + example.componentsSeparatedByString("@")[1] 
// "@student.harvard.edu" 

如果你真的只想要擴展域字符串的最後兩部分,那麼這將工作。

let example = "[email protected]" 
let domain = example.componentsSeparatedByString("@") 
let parts = domain.componentsSeparatedByString(".") 
let univId = "@" + parts[parts.count - 2] + "." + parts[parts.count - 1] 
// @harvard.edu 
+0

感謝隊友!正是我在找什麼。 –