john G john G - 2 months ago 9
ASP.NET (C#) Question

How to get a substring after certain character

i need to extract the company name from an email inside my asp.net mvc web application:-
for exmaple if i have an

email address = myeamil@mycompanyname.com


to get
Mycompanyname
with first letter capital?
BR

Answer
string address = "myeamil@mycompanyname.com";
string name = address.Split('@')[1].Split('.')[0];
name = name.Substring(0,1).ToUpper() + name.Substring(1); // Mycompanyname

Another option to get name is regular expression:

var name = Regex.Match(address, @"@([\w-]+).").Groups[1].Value
Comments