您能给我相当于C ++代码的Java吗? [英] Can you please give the java equivalent of c++ code?
本文介绍了您能给我相当于C ++代码的Java吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想检查密码是否包含特殊字符?
I just wana check if the password contain special character or not?
char[] NewPswrd = NewPswrdField.getPassword();//this line is of java
int length=NewPswrd.length;
for(int i=0;i<length;i++)
{
char* digits = "!@#$%^&*()_-+=";
while(*digits)
{
if((NewPswrd[i]==*digits++))
{
}
}
}
推荐答案
%^& *()_- + =" ;; 同时(*位) { 如果(((NewPswrd [i] == **数字++]) { } } }
%^&*()_-+="; while(*digits) { if((NewPswrd[i]==*digits++)) { } } }
与C ++不同的是Java具有正则表达式支持,尽管c ++ 11和boost库具有正则表达式支持
Java unlike C++ has regular expression support, though c++ 11 and boost library has it
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class PasswordValidator{
private Pattern pattern;
private Matcher matcher;
private final String PASSWORD_PATTERN =
"((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#
%]).{6,20})"; public PasswordValidator(){ pattern = Pattern.compile(PASSWORD_PATTERN); } /** * Validate password with regular expression * @param password password for validation * @return true valid password, false invalid password */ public boolean validate(String password){ matcher = pattern.matcher(password); return matcher.matches(); } public static void main(String arg[]) { PasswordValidator pv = new PasswordValidator(); // test 1 String pw = "dharmac1A@"; if(pv.validate (pw)) { System.out.println("Valid"); } else { System.out.println("InValid"); } // test 2 pw = "iamnotvalidpassword"; if(pv.validate (pw)) { System.out.println("Valid"); } else { System.out.println("InValid"); } // test 3 pw = "dharmatejachalla"; if(pv.validate (pw)) { System.out.println("Valid"); } else { System.out.println("InValid"); } } }
%]).{6,20})"; public PasswordValidator(){ pattern = Pattern.compile(PASSWORD_PATTERN); } /** * Validate password with regular expression * @param password password for validation * @return true valid password, false invalid password */ public boolean validate(String password){ matcher = pattern.matcher(password); return matcher.matches(); } public static void main(String arg[]) { PasswordValidator pv = new PasswordValidator(); // test 1 String pw = "dharmac1A@"; if(pv.validate (pw)) { System.out.println("Valid"); } else { System.out.println("InValid"); } // test 2 pw = "iamnotvalidpassword"; if(pv.validate (pw)) { System.out.println("Valid"); } else { System.out.println("InValid"); } // test 3 pw = "dharmatejachalla"; if(pv.validate (pw)) { System.out.println("Valid"); } else { System.out.println("InValid"); } } }
这篇关于您能给我相当于C ++代码的Java吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文