感覺(jué)應(yīng)該這樣做更合理
public class HelloWorld {
??? public static void main(String[] args) {
?? ??? ?String fileName = "HelloWorld.jav";
?? ??? ?String email = "laurenyang@imooc.com";
?? ??? ?int index = fileName.lastIndexOf(".");
?? ??? ?String prefix =fileName.substring(index);
?? ??? ?if(prefix.equals(".java")) {
?? ??? ??? ?System.out.println("Java文件名正確");
?? ??? ?} else {
?? ??? ??? ?System.out.println("Java文件名無(wú)效");
?? ??? ?}
?? ?int index2 = email.lastIndexOf("@");
?? ??? ?int index3 = email.indexOf('.');?? ?
?? ??? ?if (index2 != -1 && index3 > index2) {
?? ??? ??? ?System.out.println("郵箱格式正確");
?? ??? ?} else {
?? ??? ??? ?System.out.println("郵箱格式無(wú)效");
?? ??? ?}
?? ?}
}
2015-03-11
用正則,不超過(guò)5句代碼代替你上面所有代碼。