java is alphabetic,Java中的Character.isAlphabetic和Character.isLetter有什么区别?

What is the difference between Character.isAlphabetic() and Character.isLetter() in Java? When should one use one and when should one use the other?

解决方案

According to the API docs, isLetter() returns true if the character has any of the following general category types: UPPERCASE_LETTER (Lu), LOWERCASE_LETTER (Ll), TITLECASE_LETTER (Lt), MODIFIER_LETTER (Lm), OTHER_LETTER (Lo). If we compare isAlphabetic(), it has the same but adds LETTER_NUMBER (Nl), and also any characters having Other_Alphabetic property.

What does this mean in practice? Every letter is alphabetic, but not every alphabetic is a letter - in Java 7 (which uses Unicode 6.0.0), there are 824 characters in the BMP which are alphabetic but not letters. Some examples include 0345 (a combiner used in polytonic Greek), Hebrew vowel points (niqqud) starting at 05B0, Arabic honorifics such as saw ("peace be upon him") at 0610, Arabic vowel points... the list goes on.

But basically, for English text, the distinction makes no difference. For some other languages, the distinction might make a difference, but it is hard to predict in advance what the difference might be in practice. If one has a choice, the best answer may be isLetter() - one can always change to permit additional characters in the future, but reducing the set of accepted characters might be harder.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,可以使用Java的Comparator接口来实现按照不同位字符排序。以下是一个示例代码: ```java import java.util.Arrays; import java.util.Comparator; public class StringSorter { public static void main(String[] args) { String[] arr = {"A1", "B2", "C3", "D4", "E5", "F6", "G7", "H8", "I9", "J10"}; Arrays.sort(arr, new StringComparator()); System.out.println(Arrays.toString(arr)); } } class StringComparator implements Comparator<String> { @Override public int compare(String s1, String s2) { int len1 = s1.length(); int len2 = s2.length(); int lim = Math.min(len1, len2); for (int i = 0; i < lim; i++) { char c1 = s1.charAt(i); char c2 = s2.charAt(i); if (Character.isDigit(c1) && Character.isDigit(c2)) { int num1 = Integer.parseInt(String.valueOf(c1)); int num2 = Integer.parseInt(String.valueOf(c2)); if (num1 != num2) { return num1 - num2; } } else if (Character.isAlphabetic(c1) && Character.isAlphabetic(c2)) { if (c1 != c2) { return c1 - c2; } } else { return Character.compare(c1, c2); } } return len1 - len2; } } ``` 在上述代码,我们先定义了一个StringSorter类和一个StringComparator类。StringSorter定义了一个String类型的数组arr,数组包含了一些字符串,这些字符串由大写字母和数字组成。然后,我们使用Arrays.sort方法对这个数组进行排序,排序时需要传入一个Comparator类型的比较器,这里我们传入了我们自己定义的StringComparator对象。 在StringComparator类,我们实现了Comparator接口的compare方法。在比较两个字符串时,我们先比较它们的长度,然后按照每个字符的ASCII码值进行比较。如果两个字符都是数字,则将它们转换成数字进行比较;如果两个字符都是字母,则直接比较它们的ASCII码值;如果两个字符一个是数字一个是字母,则按照数字优先的原则进行比较。 最后,我们可以看到输出的结果是按照不同位字符排序的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值