-2

This code is supposed to parse an email into the username and domain name. If I type in apa@gmail.com, a message box displays and says "Username: apa Domain Name: gmail.com". I adapted the code from my textbook and keep getting the above error message at email = Email.Remove(0, 1); and domainName = Email.Remove(Email.Length -1, 1). Any suggestions?

public partial class StringHandling : Form
{
    string email = "";
    string state = "";
    string city = "";
    int zipCode = 0;

    public StringHandling()
    {
        InitializeComponent();
    }

    private void btnParse_Click(object sender, EventArgs e)
    {
        string Email = "";
        string domainName = "";
        email = txtEmail.Text;
        email = Email.Trim();
        if (email.StartsWith(""))
            email = Email.Remove(0, 1);
        if (email.EndsWith("@"))
            domainName = Email.Remove(Email.Length - 1, 1);

        MessageBox.Show("Username: " + email + "\n" + "Domain Name: " + domainName);
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
Amanda
  • 55
  • 8
  • 1
    `email.StartsWith("")` - that will return true for an empty string (as you're testing if the string starts with, well, an empty string) – stuartd Mar 06 '19 at 15:48
  • 4
    In C# _email_ and _Email_ are two different variables. You are assigning an empty string to the _email_ variable and of course you cannot remove anything from an empty string – Steve Mar 06 '19 at 15:48
  • `Email` is always empty so `Email.Length - 1` will always be -1 – Panagiotis Kanavos Mar 06 '19 at 15:49
  • Also I doubt that your textbook really says that this code should parse an email in its two components. This code just tries to remove characters from the beginning and the end of a string – Steve Mar 06 '19 at 15:53

1 Answers1

1

email.StartsWith("") will always be true (You've haven't actually specified anything that email should start with). Since it will always start with an empty string, when you attempt to call email = Email.Remove(0, 1);. You're attempting to remove the first character, and because the string is empty, there is no first character (The total string length is 0), so you get that exception.

My suggestion: Avoid multiple variables with different capitalization (ie. email and Email). This is leading to some confusion. These are 2 separate variables, and at no point have you assigned anything to Email (Capitol E), so in the case of email = Email.Remove(0, 1);, Email will always be an empty string and throw this error.

Blue
  • 22,608
  • 7
  • 62
  • 92
  • Okay, that I can understand. I fixed that part. Now, it's separating the user name from the domain name I can't seem to get. It's just populating everything into the username field of my message box. @FrankerZ Thank you for actually helping and not being condescending. I'm brand new to C# and the class is very difficult for me because I didn't take all the same classes as my other classmates. You're doing me a very huge favor here. I was able to qualify the user name but not the domain name, and also it can't show the @ symbol at all. So, I'm just a little lost. Thank you, again for your help. – Amanda Mar 06 '19 at 17:23