-4

I have an example of an email address like this "mynameismine@hotmail.com", I just want to obtain or cut that string value and obtain this "mynameismine", I dont want to have "@hotmail.com".

Andres
  • 53
  • 2
  • 8

3 Answers3

5

If you're going to be manipulating email addresses you're best off using the MailAddress class in the System.Net.Mail namespace:

MailAddress addr = new MailAddress("mynameismine@hotmail.com");
string username = addr.User;
string domain = addr.Host;

username is the part before the '@' symbol and domain is the part after.

Calum
  • 1,889
  • 2
  • 18
  • 36
3
var newString = emailString.Split('@').First();

split the string up by @, grab the first item.

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
2

using System.Net.Mail;

MailAddress address = new MailAddress("mynameismine@hotmail.com");
string username = address.User;

Just a class specially for these purposes.

wingerse
  • 3,670
  • 1
  • 29
  • 61