在 Java 中,不使用正则表达式来判断字符是字母还是数字的最佳方法是什么?
在不使用正则表达式的情况下,在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Character.isDigit(string.charAt(index))
(JavaDoc) 如果是数字则返回 trueCharacter.isLetter(string.charAt(index))
(JavaDoc) 如果是字母则返回 trueCharacter.isDigit(string.charAt(index))
(JavaDoc) will return true if it's a digitCharacter.isLetter(string.charAt(index))
(JavaDoc) will return true if it's a letter我正在寻找一个仅检查它是否是拉丁字母或十进制数字之一的函数。由于
char c = 255
,在可打印版本中为 ├,并被Character.isLetter(c)
视为字母。我认为这个功能是大多数开发人员正在寻找的:
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 byCharacter.isLetter(c)
.This function I think is what most developers are looking for:
正如答案所示(如果您仔细检查它们!),您的问题是不明确的。 “Az 字母”或数字是什么意思?
如果您想知道某个字符是否是 Unicode 字母或数字,请使用
Character.isLetter
和Character.isDigit
方法.如果您想知道某个字符是否是 ASCII 字母或数字,那么最好的办法是通过比较字符范围 'a' 到 'z'、'A 来进行测试' 到 'Z' 和 '0' 到 '9'。
请注意,所有 ASCII 字母/数字都是 Unicode 字母/数字...但有许多 Unicode 字母/数字字符不是 ASCII。例如,重音字母、西里尔字母、梵语……
一般的解决方案是这样做:
然后测试该块是否是您感兴趣的块之一。在某些情况下,您需要测试多个块。例如,(至少)有 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
andCharacter.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:
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.
Java Character 类有一个 isLetterOrDigit 1.0.2版本以来的方法
Java Character class has an isLetterOrDigit method since version 1.0.2
我不知道最好的,但这对我来说似乎很简单:
I don't know about best, but this seems pretty simple to me:
使用下面的代码
Character.isLetterOrDigit(string.charAt(index))
Use the below code
Character.isLetterOrDigit(string.charAt(index))
来源:https://docs.oracle.com/javase/tutorial/i18n /text/charintro.html
Source: https://docs.oracle.com/javase/tutorial/i18n/text/charintro.html
比较一下它的价值。它应该介于“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'