简体   繁体   English

C#中字符串中的字符左侧

[英]Left of a character in a string in C#

How do I get the left of "@" character from the emailID string "feedback@abc.com" in C# 如何从C#中的emailID字符串“feedback@abc.com”获取“@”字符的左侧

Thanks 谢谢

string email = "feedback@abc.comm";
int index = email.IndexOf("@");
string user = (index > 0 ? email.Substring(0, index) : "");
string username = new System.Net.Mail.MailAddress("feedback@abc.com").User;
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. 你需要做一些健全性检查,比如确保它不是空的,你确实找到了“@”符号。

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

Use the Substring and IndexOf methods: 使用SubstringIndexOf方法:

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("@"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM