In the past, there was such a question in the written test. The content was similar to the question. I knew how to do it, but I couldn't write it in the White Paper. It was immediately rejected by the Technical Manager.
This is a Java class that I have time to write for simple verification of the email format. However, regular expressions are generally used for validation of these formats, so this class can be used.
Package com. test;
Public class test ...{
/***//**
* @ Param ARGs
*/
Private string email;
Public String getemail ()...{
Return email;
}
Public void setemail (string email )...{
This. Email = Email;
}
Public Boolean isvalidate (string s )...{
Boolean flag = false;
Int index;
Int end;
// Check whether @ exists. check whether there are some special characters before @ exists.
Try ...{
If (S. indexof ("@")> 0 )...{
Index = S. indexof ("@");
End = S. Length ();
System. Out. println ("@ index is:" + index + "end:" + end );
Int index_2 = S. indexof ("@", index );
// Whether multiple @ exists @
If (index_2! =-1 )...{
// @ Check whether there are any characters in front of the string, as shown in the following code. If the character string is null, you can write a special method. I won't be arrogant here.
If (index <(end-1 ))...{
// Obtain the string following @
S = S. substring (index + 1 );
Int end_2 = 0;
While (S. indexof (".")! =-1 )...{
Index_2 = S. indexof (".");
End_2 = S. Length ();
System. Out. println (". index is:" + index_2 );
// Whether a. exists
If (index_2! =-1 )...{
// Obtain the first string.
String points = S. substring (0, index_2 );
System. Out. println ("befor.:" + points );
// Judge whether the string @ to. Is null
If (! Points. Equals (""))...{
// Determine whether a string exists in the backend.
If (index_2 <(end_2-1 ))...{
S = S. substring (index_2 + 1 );
System. Out. println ("after.:" + S );
Flag = true;
}
Else ...{
Flag = false;
Break;
}
}
}
}
}
}
}
} Catch (stringindexoutofboundsexception SE )...{
Se. printstacktrace ();
Return false;
}
Return flag;
}
Public static void main (string [] ARGs )...{
// Todo auto-generated method stub
Test T = new test ();
T. setemail ("123@1.12 ");
If (T. isvalidate (T. getemail ()))...{
System. Out. println ("OK ");
}
Else ...{
System. Out. println ("false ");
}
}
}