如何确定字符串是否包含非字母数字字符? [英] How to determine if a String has non-alphanumeric characters?

查看:956
本文介绍了如何确定字符串是否包含非字母数字字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个方法来告诉我一个字符串是否有非字母数字字符。

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

例如,如果String是abcdef?或abcdefà,该方法必须返回true。

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

推荐答案

使用Apache Commons Lang:

Using Apache Commons Lang:

!StringUtils.isAlphanumeric(String)

对String的字符进行交替迭代,并检查:

Alternativly iterate over String's characters and check with:

!Character.isLetterOrDigit(char)

您还有一个问题:
您的示例字符串abcdefà是字母数字的,因为à是一封信。

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();

这篇关于如何确定字符串是否包含非字母数字字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆