如何在 Java 中验证字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3732809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:04:18  来源:igfitidea点击:

How can a string be validated in Java?

javastringvalidation

提问by yash

How can a string be validated in Java? I.e. only characters are allowed and not numbers? How about email validation?

如何在 Java 中验证字符串?即只允许字符而不是数字?电子邮件验证怎么样?

回答by jsshah

for string with only characters try

对于只有字符的字符串尝试

private boolean verifyLastName(String lname)
{
    lname = lname.trim();

    if(lname == null || lname.equals(""))
        return false;

    if(!lname.matches("[a-zA-Z]*"))
        return false;

    return true;
}

for email validation try

用于电子邮件验证尝试

private boolean verifyEmail(String email)
{
    email = email.trim();

    if(email == null || email.equals(""))
        return false;

    if(!email.matches("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$"))
        return false;

    return true;
}

回答by NullUserException

how a string can be validated in java?

如何在java中验证字符串?

A common way to do that is by using a regex, or Regular Expression. In Java you can use the String.matches(String regex)method. With regexes we say you match a string against a patternIf a match is successful, .matches()returns true.

一种常见的方法是使用正则表达式或正则表达式。在 Java 中,您可以使用该String.matches(String regex)方法。对于正则表达式,我们说您将字符串与模式匹配 如果匹配成功,则.matches()返回 true。



only characters allowed and not numbers?

只允许字符而不是数字?

// You can use this pattern:
String regex = "^[a-zA-Z]+$";
if (str.matches(regex)) { 
    // ...
}

email validation?

电子邮件验证?

Email addresses have a very complicated spec, which requires a monstrous regex to be accurate. This one is decent and short, but not exactly right:

电子邮件地址有一个非常复杂的规范,它需要一个可怕的正则表达式才能准确。这是一个体面而简短的,但并不完全正确:

String regex = "\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b";
if (str.matches(regex)) { 
    // ...
}

If you really want to look into it: How to validate emailand Comparing email validating regexes(PHP).

如果您真的想研究它:如何验证电子邮件比较电子邮件验证正则表达式(PHP)。



Here's an excellent resource to get started on regex:
http://www.regular-expressions.info

这是开始使用正则表达式的绝佳资源:http:
//www.regular-expressions.info

回答by Pawe? Dyda

For simple cases like that, go with RegExp as NullUserException already suggested. If you need more robust solution you can use some validation framework, i.e. Apache Commons Validator.

对于像这样的简单情况,请使用 RegExp 作为 NullUserException 的建议。如果您需要更强大的解决方案,您可以使用一些验证框架,即Apache Commons Validator

回答by gpeche

If you have Apache commons-langas a project dependency (and that is quite usual), you can use StringUtils.isAlpha(). If you have a more specific validation or do not want a dependency on commons-lang, you can do it with regular expressions and String.matches().

如果您将Apache commons-lang作为项目依赖项(这很常见),您可以使用StringUtils.isAlpha(). 如果您有更具体的验证或不想依赖 commons-lang,则可以使用正则表达式和String.matches().