我是靠谱客的博主 苹果店员,最近开发中收集的这篇文章主要介绍java中digit什么意思_Character.digit()的用法,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.在电话拨号盘PhoneNumberUtils.java有一段代码如下:/**

* Strips separators from a phone number string.

* @param phoneNumber phone number to strip.

* @return phone string stripped of separators.

*/

public static String stripSeparators(String phoneNumber) {

if (phoneNumber == null) {

return null;

}

int len = phoneNumber.length();

StringBuilder ret = new StringBuilder(len);

for (int i = 0; i < len; i++) {

char c = phoneNumber.charAt(i);

// Character.digit() supports ASCII and Unicode digits (fullwidth, Arabic-Indic, etc.)

int digit = Character.digit(c, 10);

if (digit != -1) {

ret.append(digit);

} else if (isNonSeparator(c)) {

ret.append(c);

}

}

return ret.toString();

}

其中有一个API是:

int digit = Character.digit(c, 10);

这个大概是把一个字符Char类型转化为一个整数.但是是转成什么样子了?

2.验证过程

官方的解释是:

public static int digit(char ch,int radix)

Returns the numeric value of the character ch in the specified radix.

If the radix is not in the range MIN_RADIX ≤ radix ≤ MAX_RADIX or if the value of ch is not a valid digit in the specified radix, -1 is returned. A character is a valid digit if at least one of the following is true:

The method isDigit is true of the character and the Unicode decimal digit value of the character (or its single-character decomposition) is less than the specified radix. In this case the decimal digit value is returned.

The character is one of the uppercase Latin letters 'A' through 'Z' and its code is less than radix + 'A' - 10. In this case, ch - 'A' + 10 is returned.

The character is one of the lowercase Latin letters 'a' through 'z' and its code is less than radix + 'a' - 10. In this case, ch - 'a' + 10 is returned.

The character is one of the fullwidth uppercase Latin letters A ('uFF21') through Z ('uFF3A') and its code is less than radix + 'uFF21' - 10. In this case, ch - 'uFF21' + 10 is returned.

The character is one of the fullwidth lowercase Latin letters a ('uFF41') through z ('uFF5A') and its code is less than radix + 'uFF41' - 10. In this case, ch - 'uFF41' + 10 is returned.

Note: This method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the digit(int, int) method.

Parameters:

ch - the character to be converted.

radix - the radix.

Returns:

the numeric value represented by the character in the specified radix.

See Also:

forDigit(int, int), isDigit(char)

大概猜出了是把字符类型转化为radix进制的整数.比如radix为10就转为10进制的,16就是16进制的.如果该字符不是进制内的就返回-1.

验证的代码如下:

package Say;

public class Diagi {

public static void main(String[] args) {

char num1 = '0';

char num2 = '9';

char a = 'A';

char as = 'a';

char f = 'F';

char fs = 'f';

System.out.println("result:" + Character.digit(num1, 16));

System.out.println("result:" + Character.digit(num2, 16));

System.out.println("result:" + Character.digit(a, 16));

System.out.println("result:" + Character.digit(as, 16));

System.out.println("result:" + Character.digit(f, 16));

System.out.println("result:" + Character.digit(fs, 16));

System.out.println("result:" + Character.digit('H', 16));

}

}

结果如下:

result:0

result:9

result:10

result:10

result:15

result:15

result:-1

验证的结果也证实了猜测.

3.stripSeparator函数的意思

该函数是把拨号盘输入的字符,格式化成10进制数,如果有字母将会被忽略掉.

最后

以上就是苹果店员为你收集整理的java中digit什么意思_Character.digit()的用法的全部内容,希望文章能够帮你解决java中digit什么意思_Character.digit()的用法所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部