-1

I think what I want to do is rather simple, but not sure. I'd like to do this in the easiest way as I have multiple different variables I want to apply this to. I want to search if a variable contains a certain word or words and then if it does set a new variable. For example if someone signs up for email and they have Gmail, I want to set a new variable for use later. I'm self taught and obviously new to java and would appreciate any feedback. Thanks.

if (email == "*@gmail.com") 
{
    var mailtype = "gmail"
};

Another example would be to verify email address format:

if (email == "*@*.*") 
{
    var mailformat = "okay"
};
Md Ashaduzzaman
  • 4,032
  • 2
  • 18
  • 34
DMatB
  • 9
  • 1

1 Answers1

1

The simplest way, since you are just getting started, to do this is:

if( email.toLowerCase().endsWith("@gmail.com") ) {
  mailtype = "gmail";
}
else if( email.toLowerCase().endsWith("@yahoo.com") ) {
  mailtype = "yahoo";
}
else {
  mailtype = "okay";
}

But this being said, you will want to take a look at regular expressions and learn how to do this in a more solid and expandable way with matching groups.

mprivat
  • 21,582
  • 4
  • 54
  • 64