将单词与其他文本匹配

时间:2014-11-24 17:02:08

标签: java netbeans

我正在制作一个程序,该程序会将用户数据用于为帐户注册。对于其中一个部分,我想在他们选择的密码中有一个类似于用户名的单词时发出错误消息。例如:username = Johnpassword = 5john123。错误!您的密码不能包含用户名。我正在查看过去的问题,我找到了一个帮助我的答案。但只有一点。有人建议的代码只是这样做,如果密码和用户名完全相同,那么它会显示错误消息。这就是他们的建议:

if (Arrays.asList(password.split("[\\s]")).indexOf(name) != -1)  
              System.out.println("Error!  Your password cannot include your username");   
              else            
              System.out.println("valid password");

以上代码仅在密码和用户名相同时才有效。如果任何一方都添加了任何内容,它就无法运作。

如何修改此选项,以便无论是否在任何一方添加了数字,它仍然会发现用户名是否包含在密码中?谢谢。

1 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

String userName = "John";
String password = "5john123";
if (password.toLowerCase().contains(userName.toLowerCase())) {
    System.out.println("Error! Your password cannot include your username");
} else {
    System.out.println("valid password");
}
相关问题