如何确定字符串是否包含非字母数字字符?

发布于 2024-12-17 20:01:23 字数 84 浏览 3 评论 0原文

我需要一种方法来告诉我字符串是否包含非字母数字字符。

例如,如果字符串是“abcdef?”或“abcdefà”,该方法必须返回 true。

I need a method that can tell me if a String has non alphanumeric characters.

For example if the String is "abcdef?" or "abcdefà", the method must return true.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

野味少女 2024-12-24 20:01:23

使用 Apache Commons Lang:

!StringUtils.isAlphanumeric(String)

或者迭代 String 的字符并检查:

!Character.isLetterOrDigit(char)

您还剩下一个问题:
您的示例字符串“abcdefà”是字母数字,因为 à 是一个字母。但我认为您希望它被视为非字母数字,对吗?!

所以你可能想使用正则表达式:

String s = "abcdefà";
Pattern p = Pattern.compile("[^a-zA-Z0-9]");
boolean hasSpecialChar = p.matcher(s).find();

Using Apache Commons Lang:

!StringUtils.isAlphanumeric(String)

Alternativly iterate over String's characters and check with:

!Character.isLetterOrDigit(char)

You've still one problem left:
Your example string "abcdefà" is alphanumeric, since à is a letter. But I think you want it to be considered non-alphanumeric, right?!

So you may want to use regular expression instead:

String s = "abcdefà";
Pattern p = Pattern.compile("[^a-zA-Z0-9]");
boolean hasSpecialChar = p.matcher(s).find();
但可醉心 2024-12-24 20:01:23

一种方法是使用 String 类本身来做到这一点。
假设您的字符串是这样的:

String s = "some text";
boolean hasNonAlpha = s.matches("^.*[^a-zA-Z0-9 ].*$");

另一个是使用外部库,例如 Apache commons:

String s = "some text";
boolean hasNonAlpha = !StringUtils.isAlphanumeric(s);

One approach is to do that using the String class itself.
Let's say that your string is something like that:

String s = "some text";
boolean hasNonAlpha = s.matches("^.*[^a-zA-Z0-9 ].*$");

one other is to use an external library, such as Apache commons:

String s = "some text";
boolean hasNonAlpha = !StringUtils.isAlphanumeric(s);
去了角落 2024-12-24 20:01:23

您必须遍历字符串中的每个字符并检查 Character.isDigit(char);Character.isletter(char);

或者,您可以使用正则表达式。

You have to go through each character in the String and check Character.isDigit(char); or Character.isletter(char);

Alternatively, you can use regex.

冰魂雪魄 2024-12-24 20:01:23

使用此函数检查字符串是否为字母数字:

public boolean isAlphanumeric(String str)
{
    char[] charArray = str.toCharArray();
    for(char c:charArray)
    {
        if (!Character.isLetterOrDigit(c))
            return false;
    }
    return true;
}

它节省了导入外部库的麻烦,并且如果您以后希望对字符串执行不同的验证检查,可以轻松修改代码。

Use this function to check if a string is alphanumeric:

public boolean isAlphanumeric(String str)
{
    char[] charArray = str.toCharArray();
    for(char c:charArray)
    {
        if (!Character.isLetterOrDigit(c))
            return false;
    }
    return true;
}

It saves having to import external libraries and the code can easily be modified should you later wish to perform different validation checks on strings.

旧人九事 2024-12-24 20:01:23

如果您可以使用 Apache Commons 库,那么 Commons-Lang StringUtils 有一个名为 isAlphanumeric() 的方法,可以满足您的需求。

If you can use the Apache Commons library, then Commons-Lang StringUtils has a method called isAlphanumeric() that does what you're looking for.

浴红衣 2024-12-24 20:01:23

string.matches("^\\W*$"); 应该做你想要的,但它不包含空格。 string.matches("^(?:\\W|\\s)*$"); 也匹配空格。

string.matches("^\\W*$"); should do what you want, but it does not include whitespace. string.matches("^(?:\\W|\\s)*$"); does match whitespace as well.

稚气少女 2024-12-24 20:01:23

您可以使用 Java.lang 中的 Character 类的 isLetter(char c) 静态方法。

public boolean isAlpha(String s) {
    char[] charArr = s.toCharArray();

    for(char c : charArr) {
        if(!Character.isLetter(c)) {
            return false;
        }
    }
    return true;
}

You can use isLetter(char c) static method of Character class in Java.lang .

public boolean isAlpha(String s) {
    char[] charArr = s.toCharArray();

    for(char c : charArr) {
        if(!Character.isLetter(c)) {
            return false;
        }
    }
    return true;
}
懒的傷心 2024-12-24 20:01:23

虽然它不适用于数字,但您可以检查小写和大写值是否相同,对于非字母字符它们将相同,您应该在此之前检查数字以获得更好的可用性

Though it won't work for numbers, you can check if the lowercase and uppercase values are same or not, For non-alphabetic characters they will be same, You should check for number before this for better usability

宫墨修音 2024-12-24 20:01:23

我必须检查一个字符串至少包含 1 个字母和 1 个数字 - 我的解决方案如下

/**
 * <p>Checks if the String contains both letters and digits.</p>
 *
 * <p>{@code null} will return {@code false}.
 * An empty String str.isEmpty() will return {@code false}.</p>
 *
 * @param str the String to check, may be null
 * @return {@code true} if only contains letters and digits,
 * and is non-null or not empty
 */
public static boolean isAlphaAndNumeric(String str) {
    if (str == null || str.isEmpty()) {
        return false;
    }

    Pattern pattern = Pattern.compile("^[a-zA-Z0-9]*$");
    boolean containsSpecialChars = !pattern.matcher(str).matches();
    if (containsSpecialChars) {
        return false;
    }
    
    boolean containsLetter = false;
    boolean containsDigit = false;
    char[] chars = str.toCharArray();
    int i = 0;
    while (i < chars.length) {
        if (Character.isLetter(chars[i])) {
            containsLetter = true;
        }
        if (Character.isDigit(chars[i])) {
            containsDigit = true;
        }
        i++;
        if (containsDigit && containsLetter) {
            return true;
        }
    }
    return false;
}

I had to check a string contains at least 1 letter and 1 digit - my solution below

/**
 * <p>Checks if the String contains both letters and digits.</p>
 *
 * <p>{@code null} will return {@code false}.
 * An empty String str.isEmpty() will return {@code false}.</p>
 *
 * @param str the String to check, may be null
 * @return {@code true} if only contains letters and digits,
 * and is non-null or not empty
 */
public static boolean isAlphaAndNumeric(String str) {
    if (str == null || str.isEmpty()) {
        return false;
    }

    Pattern pattern = Pattern.compile("^[a-zA-Z0-9]*
quot;);
    boolean containsSpecialChars = !pattern.matcher(str).matches();
    if (containsSpecialChars) {
        return false;
    }
    
    boolean containsLetter = false;
    boolean containsDigit = false;
    char[] chars = str.toCharArray();
    int i = 0;
    while (i < chars.length) {
        if (Character.isLetter(chars[i])) {
            containsLetter = true;
        }
        if (Character.isDigit(chars[i])) {
            containsDigit = true;
        }
        i++;
        if (containsDigit && containsLetter) {
            return true;
        }
    }
    return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文