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

发布于 2024-09-29 05:52:34 字数 82 浏览 6 评论 0原文

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

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.

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

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

发布评论

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

评论(9

清泪尽 2024-10-06 05:52:34

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

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

惯饮孤独 2024-10-06 05:52:34

我正在寻找一个仅检查它是否是拉丁字母或十进制数字之一的函数。由于 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');
}

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:

private static boolean isLetterOrDigit(char c) {
    return (c >= 'a' && c <= 'z') ||
           (c >= 'A' && c <= 'Z') ||
           (c >= '0' && c <= '9');
}
猫烠⑼条掵仅有一顆心 2024-10-06 05:52:34

正如答案所示(如果您仔细检查它们!),您的问题是不明确的。 “Az 字母”或数字是什么意思?

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

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

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


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

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

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

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

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?

  • If you want to know if a character is a Unicode letter or digit, then use the Character.isLetter and Character.isDigit methods.

  • If you want to know if a character is an ASCII letter 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'.

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, ...


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.UnicodeBlock class defines static constants for well-known blocks; see the javadocs.

Note that any code point will be in at most one block.

归途 2024-10-06 05:52:34

Java Character 类有一个 isLetterOrDigit 1.0.2版本以来的方法

Java Character class has an isLetterOrDigit method since version 1.0.2

双马尾 2024-10-06 05:52:34

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

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

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

Character.isDigit(str.charAt(index))
Character.isLetter(str.charAt(index))
始于初秋 2024-10-06 05:52:34

使用下面的代码

Character.isLetterOrDigit(string.charAt(index))

Use the below code

Character.isLetterOrDigit(string.charAt(index))

明媚殇 2024-10-06 05:52:34
// 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'))
    // ...

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

// 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

花开半夏魅人心 2024-10-06 05:52:34

比较一下它的价值。它应该介于“a”和“z”、“A”和“Z”、“0”和“9”之间

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

听风念你 2024-10-06 05:52:34
 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");


}
}
 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");


}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文