开发者

Left of a character in a string in C#

开发者 https://www.devze.com 2023-01-02 10:28 出处:网络
How do I get the left of \"@\" character from the emailID string \"feedback@abc.com\" in C#开发者_如何转开发

How do I get the left of "@" character from the emailID string "feedback@abc.com" in C#

开发者_如何转开发

Thanks


string email = "feedback@abc.comm";
int index = email.IndexOf("@");
string user = (index > 0 ? email.Substring(0, index) : "");


var email = "feedback@abc.com";
var name = email.Substring(0, email.IndexOf("@"));

You'll want to do some sanity checks like make sure it's not null and that you actually find the "@" sign.


string username = new System.Net.Mail.MailAddress("feedback@abc.com").User;


email.Split('@').Last() email.Split('@').First()


Use the Substring and IndexOf methods:

var email = "feedback@abc.com";
var user = email.Substring(0, email.IndexOf("@"))


Something like this should work.

string email = "test@testdomain.com";
string user = email.Substring(0, email.IndexOf("@"));
0

精彩评论

暂无评论...
验证码 换一张
取 消