接收用户输入的邮箱名,获取邮箱的名字
比如:cxy@163.com,输出cxy
import java.util.*;
public class email {
public static void main(String[] args) {
System.out.print("请输入邮箱:");
String email = new Scanner(System.in).nextLine();
getName(email);
}
public static void getName(String email){
int index = email.indexOf("@");
// xxxxx@qq.com
// /
//定位到@的位置
if (index == -1) {//未找到@的值为-1
System.out.println("邮箱格式错误");
}
System.out.println("邮箱用户名:"+email.substring(0,index));
// substring(a,b)截取a到b的片段
}
}