我正在尝试建立一个程序
@
符号,然后.edu
教育电子邮件地址的一部分,最后@school.edu
部分,然后返回其余部分。我尝试使用charAt
,但是我一直收到不兼容类型错误,并且我不确定如何删除每次可能位于不同位置的字符串部分。任何指导都将受到欢迎。
这是我到目前为止所拥有的:
if (UserEmail.charAt(0) == (".edu"))
String UserName = UserEmail.substring(0,//location of @//)
else
System.out.print(UserEmail + "is not an acceptable email address.
System.out.print("Type your email address.");
UserEmail = kb.nextLine();
您可以使用string.replaceAll
功能。
string.replaceAll("@\\S+?\\.edu\\b", "");
\\S+?
将对一个或多个非空格字符进行非贪婪匹配。
例子:
String r = "[email protected] bar";
System.out.println(r.replaceAll("@\\S+?\\.edu\\b", ""));
输出:
foo bar
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句