2010-06-04 18 views

回答

7
string email = "[email protected]"; 
int index = email.IndexOf("@"); 
string user = (index > 0 ? email.Substring(0, index) : ""); 
3
var email = "[email protected]"; 
var name = email.Substring(0, email.IndexOf("@")); 

你」我們希望做一些理智的檢查,例如確保它不爲空,並且確實找到了「@」符號。

1

像這樣的東西應該工作。

string email = "[email protected]"; 
string user = email.Substring(0, email.IndexOf("@")); 
3

email.Split( '@')最近() email.Split( '@')第一()

2

使用SubstringIndexOf方法:。

var email = "[email protected]"; 
var user = email.Substring(0, email.IndexOf("@")) 
+0

小寫** b **在字符串上。 'Substring'不是_SubString_。 – 2016-10-20 15:01:04

+1

@MattBaker編輯正確的外殼 – goric 2016-10-20 15:33:40