简体   繁体   English

检查字符串中的字母字符

[英]Check String for Alphabetical Characters

If I have the strings "hello8459" and "1234", how would I go about detecting which one had the alphabetical characters in? 如果我有字符串“hello8459”和“1234”,我将如何检测哪个字符串中包含字母字符? I've been trying: 我一直在努力:

//Checking for numerics in an if...
Pattern.matches("0-9", string1);

However it doesn't work at all. 但它根本不起作用。 Can anyone advise? 任何人都可以建议吗?

On one hand you are asking how to detect alphabet chars, but your code seems to be attempting to detect numerics. 一方面,你问的是如何检测字母表字符,但你的代码似乎试图检测数字。

For numerics: 对于数字:

[0-9]

For alphabet: 对于字母:

[a-zA-Z]

"0-9" matches just that, the string: "0-9". “0-9”只匹配,字符串:“0-9”。 What you probably meant to do is "[0-9]+" which matches one ore more digits. 您可能要做的是“[0-9] +”,它匹配一个或多个数字。

And you can use String's matches(...) method: 你可以使用String的match(...)方法:

boolean onlyDigits = "1234578".matches("\\d+");

Be careful though, when parsing to a primitive int or long after checking 'onlyDigits': it might be a large number like 123145465124657897456421345487454 which does not fit in a primitive data type but passes the matches(...) test! 但是,在检查'onlyDigits'之后解析为原始int长时间时要小心:它可能是一个很大的数字,如123145465124657897456421345487454,它不适合原始数据类型但通过匹配(...)测试!

In Ruby there is method in String class called "string".is_alphabet? 在Ruby中,String类中的方法名为“string”.is_alphabet? which tells that if string contains only alphabet character or not. 这告诉我们,如果字符串只包含字母字符。 But Unfortunately in java there is no method to check if a string contains only alphabetic character in it,But No worries you can do something like 但不幸的是在java中没有方法来检查字符串中是否只包含字母字符,但不用担心你可以做类似的事情

   boolean isAlphabet = "1234".matches("[a-zA-Z]+") which'll returns false
   boolean isAlphabet = "hello".matches("[a-zA-Z]+") which'll return true cause it contains only alphabet.

Here is an alternative, not involving regex at all: 这是一个替代方案,根本不涉及正则表达式:

try {
    Integer.parseInt( input );
    // Input is numeric
}
catch( Exception ) {
    // Input is not numeric
}

You don't want to check whether there are any numbers, you want to check whether there is anything that is not a number (or at least that is what your question says, maybe not what you meant). 你不想检查是否有任何数字,你想检查是否有任何不是数字的东西(或者至少是你的问题所说的,也许不是你的意思)。 So you want to negate your character class and search for the presence of anything that is not a digit, instead of trying to find anything that is a digit. 所以你想要否定你的角色类并搜索任何不是数字的东西,而不是试图找到任何数字。 There's also the empty string of course. 当然还有空字符串。 In code: 在代码中:

boolean isNumeric = !(str.matches("[^0-9]") || "".equals(str));

A way to check all of the characters in a string is to turn it into a char array: 检查字符串中所有字符的方法是将其转换为char数组:

char[] check = yourstring.toCharArray();

And then make a for loop that checks all of the characters individually: 然后创建一个for循环,分别检查所有字符:

for(int i=0; i < check.length; i++){
    if(!Character.isDigit(check[i])){
        System.out.println("Not numberal");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM