在不使用正则表达式的情况下,在 Java 中判断字符是字母还是数字的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4047808/
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 10:55:35  来源:igfitidea点击:

What is the best way to tell if a character is a letter or number in Java without using regexes?

javacharnumbersletter

提问by Daniel Sopel

What is the best and/or easiest way to recognize if a string.charAt(index) is an A-z letter or a number in Java without using regular expressions? Thanks.

在不使用正则表达式的情况下识别 string.charAt(index) 是 Java 中的 Az 字母还是数字的最佳和/或最简单的方法是什么?谢谢。

采纳答案by Adam

Character.isDigit(string.charAt(index))(JavaDoc) will return true if it's a digit
Character.isLetter(string.charAt(index))(JavaDoc) will return true if it's a letter

Character.isDigit(string.charAt(index))( JavaDoc) 如果是数字则返回 true
Character.isLetter(string.charAt(index))( JavaDoc) 如果是字母则返回 true

回答by YuppieNetworking

Compare its value. It should be between the value of 'a' and 'z', 'A' and 'Z', '0' and '9'

比较它的价值。它应该在 'a' 和 'z'、'A' 和 'Z'、'0' 和 '9' 的值之间

回答by Cameron

I don't know about best, but this seems pretty simple to me:

我不知道最好的,但这对我来说似乎很简单:

Character.isDigit(str.charAt(index))
Character.isLetter(str.charAt(index))

回答by Stephen C

As the answers indicate (if you examine them carefully!), your question is ambiguous. What do you mean by "an A-z letter" or a digit?

正如答案所表明的(如果你仔细检查它们!),你的问题是模棱两可的。“Az 字母”或数字是什么意思?

  • If you want to know if a character is a Unicodeletter or digit, then use the Character.isLetterand Character.isDigitmethods.

  • If you want to know if a character is an ASCIIletter or digit, then the best thing to do is to test by comparing with the character ranges 'a' to 'z', 'A' to 'Z' and '0' to '9'.

  • 如果您想知道一个字符是Unicode字母还是数字,请使用Character.isLetterCharacter.isDigit方法。

  • 如果你想知道一个字符是ASCII字母还是数字,那么最好的办法是通过比较字符范围 'a' 到 'z'、'A' 到 'Z' 和 '0' 来测试'9'。

Note that all ASCII letters / digits are Unicode letters / digits ... but there are many Unicode letters / digits characters that are not ASCII. For example, accented letters, cyrillic, sanskrit, ...

请注意,所有 ASCII 字母/数字都是 Unicode 字母/数字……但有许多 Unicode 字母/数字字符不是 ASCII。例如,重音字母、西里尔字母、梵文、...



The general solution is to do this:

一般的解决方案是这样做:

Character.UnicodeBlock block = Character.UnicodeBlock.of(someCodePoint);

and then test to see if the block is one of the ones that you are interested in. In some cases you will need to test for multiple blocks. For example, there are (at least) 4 code blocks for Cyrillic characters and 7 for Latin. The Character.UnicodeBlockclass defines static constants for well-known blocks; see the javadocs.

然后测试该块是否是您感兴趣的块之一。在某些情况下,您需要测试多个块。例如,西里尔字母有(至少)4 个代码块,拉丁文有 7 个代码块。在Character.UnicodeBlock类定义公知的块静态常量; 请参阅javadocs

Note that any code point will be in at mostone block.

请注意,任何代码点最多位于一个块中。

回答by ChuanRocks

Java Character class has an isLetterOrDigitmethod since version 1.0.2

Java Character 类从 1.0.2 版本开始就有一个isLetterOrDigit方法

回答by mr5

I'm looking for a function that checks only if it's one of the Latin letters or a decimal number. Since char c = 255, which in printable version is and considered as a letter by Character.isLetter(c). This function I think is what most developers are looking for:

我正在寻找一个函数,该函数仅检查它是拉丁字母之一还是十进制数。因为char c = 255,在可打印版本中是并被视为一个字母Character.isLetter(c)。我认为这个功能是大多数开发人员正在寻找的:

private static boolean isLetterOrDigit(char c) {
    return (c >= 'a' && c <= 'z') ||
           (c >= 'A' && c <= 'Z') ||
           (c >= '0' && c <= '9');
}

回答by vadasambar

// check if ch is a letter
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
    // ...

// check if ch is a digit
if (ch >= '0' && ch <= '9')
    // ...

// check if ch is a whitespace
if ((ch == ' ') || (ch =='\n') || (ch == '\t'))
    // ...

Source: https://docs.oracle.com/javase/tutorial/i18n/text/charintro.html

来源:https: //docs.oracle.com/javase/tutorial/i18n/text/charintro.html

回答by Gowtham Prasath

 import java.util.Scanner;
 public class v{
 public static void main(String args[]){
 Scanner in=new Scanner(System.in);
    String str;
    int l;
    int flag=0;
    System.out.println("Enter the String:");
    str=in.nextLine();
    str=str.toLowerCase();
    str=str.replaceAll("\s","");
    char[] ch=str.toCharArray();
    l=str.length();
    for(int i=0;i<l;i++){
        if ((ch[i] >= 'a' && ch[i]<= 'z') || (ch[i] >= 'A' && ch[i] <= 'Z')){
        flag=0;
        }
        else

        flag++;
        break;
        } 
if(flag==0)
    System.out.println("Onlt char");


}
}

回答by Ram Repaka

Use the below code

使用下面的代码

Character.isLetterOrDigit(string.charAt(index))

Character.isLetterOrDigit(string.charAt(index))