杰瑞科技汇

Java中如何处理ASCII字符?

Of course! In Java, working with ASCII characters is a fundamental task. Since Java uses the Unicode character set (UTF-16), every char is technically a Unicode code point. However, the first 128 characters of Unicode are identical to the ASCII standard, which makes handling ASCII straightforward.

Java中如何处理ASCII字符?-图1
(图片来源网络,侵删)

Here’s a comprehensive guide covering the key aspects of working with ASCII characters in Java.

The char Primitive Type

The primary data type for a single character in Java is char. It's a 16-bit unsigned value that can hold any character from the Unicode set.

char myChar = 'A'; // A single character literal
char myDigit = '7';
char mySymbol = '@';

ASCII Values (Casting to int)

In Java, you can easily convert a char to its underlying numeric value (which is its Unicode/ASCII code point) by casting it to an int.

public class AsciiExample {
    public static void main(String[] args) {
        char letter = 'A';
        char digit = '7';
        // Cast char to int to get its ASCII/Unicode value
        int asciiValueOfA = (int) letter;
        int asciiValueOf7 = (int) digit;
        System.out.println("The character '" + letter + "' has an ASCII value of: " + asciiValueOfA);
        System.out.println("The character '" + digit + "' has an ASCII value of: " + asciiValueOf7);
    }
}

Output:

Java中如何处理ASCII字符?-图2
(图片来源网络,侵删)
The character 'A' has an ASCII value of: 65
The character '7' has an ASCII value of: 55

Creating a Character from an ASCII Value (Casting char)

You can also do the reverse: create a char from an integer ASCII value by casting the int to a char.

public class AsciiToChar {
    public static void main(String[] args) {
        int asciiCode = 65;
        int anotherCode = 97;
        // Cast int to char to get the character
        char charFromCode = (char) asciiCode;
        char anotherChar = (char) anotherCode;
        System.out.println("ASCII code " + asciiCode + " is the character: " + charFromCode);
        System.out.println("ASCII code " + anotherCode + " is the character: " + anotherChar);
    }
}

Output:

ASCII code 65 is the character: A
ASCII code 97 is the character: a

Checking if a Character is ASCII

A common task is to verify if a given character falls within the ASCII range. ASCII characters have values from 0 to 127.

public class IsAscii {
    public static void main(String[] args) {
        char char1 = 'Z';      // ASCII
        char char2 = 'é';      // Not ASCII (part of extended Latin-1)
        char char3 = '5';      // ASCII
        char char4 = '$';      // ASCII
        char char5 = '\u20AC'; // Euro sign, not ASCII
        System.out.println("Is 'Z' ASCII? " + isAscii(char1));
        System.out.println("Is 'é' ASCII? " + isAscii(char2));
        System.out.println("Is '5' ASCII? " + isAscii(char3));
        System.out.println("Is '$' ASCII? " + isAscii(char4));
        System.out.println("Is '€' ASCII? " + isAscii(char5));
    }
    /**
     * Checks if a character is within the standard ASCII range (0-127).
     * @param c The character to check.
     * @return true if the character is ASCII, false otherwise.
     */
    public static boolean isAscii(char c) {
        return c <= 127;
    }
}

Output:

Java中如何处理ASCII字符?-图3
(图片来源网络,侵删)
Is 'Z' ASCII? true
Is 'é' ASCII? false
Is '5' ASCII? true
Is '$' ASCII? true
Is '€' ASCII? false

Common ASCII Categories and Character Class

The java.lang.Character class provides a wealth of utility methods for checking character properties. These are extremely useful for categorizing ASCII characters.

public class CharacterChecks {
    public static void main(String[] args) {
        char upper = 'K';
        char lower = 'k';
        char number = '9';
        char space = ' ';
        char punct = '!';
        // Note: These methods work for the entire Unicode set,
        // but they correctly identify the ASCII categories.
        System.out.println("Is '" + upper + "' an uppercase letter? " + Character.isUpperCase(upper));
        System.out.println("Is '" + lower + "' a lowercase letter? " + Character.isLowerCase(lower));
        System.out.println("Is '" + number + "' a digit? " + Character.isDigit(number));
        System.out.println("Is '" + space + "' a whitespace? " + Character.isWhitespace(space));
        System.out.println("Is '" + punct + "' a letter or digit? " + !Character.isLetterOrDigit(punct));
    }
}

Output:

Is 'K' an uppercase letter? true
Is 'k' a lowercase letter? true
Is '9' a digit? true
Is ' ' a whitespace? true
Is '!' a letter or digit? true

Converting Between Cases

You can convert between uppercase and lowercase ASCII characters using the Character class methods.

public class CaseConversion {
    public static void main(String[] args) {
        char lowerChar = 'a';
        char upperChar = 'B';
        char toUpper = Character.toUpperCase(lowerChar);
        char toLower = Character.toLowerCase(upperChar);
        System.out.println("Uppercase of '" + lowerChar + "' is: " + toUpper);
        System.out.println("Lowercase of '" + upperChar + "' is: " + toLower);
    }
}

Output:

Uppercase of 'a' is: A
Lowercase of 'B' is: b

ASCII Table (Printable Characters)

Here is a reference for the printable ASCII characters (values 32 to 126). This is what you'll most commonly work with.

Dec Hex Char Dec Hex Char Dec Hex Char Dec Hex Char
32 20 ` ` 56 38 8 80 50 P 104 68 h
33 21 57 39 9 81 51 Q 105 69 i
34 22 58 3A 82 52 R 106 6A j
35 23 59 3B 83 53 S 107 6B k
36 24 60 3C < 84 54 T 108 6C l
37 25 61 3D 85 55 U 109 6D m
38 26 & 62 3E > 86 56 V 110 6E n
39 27 63 3F 87 57 W 111 6F o
40 28 64 40 88 58 X 112 70 p
41 29 65 41 A 89 59 Y 113 71 q
42 2A 66 42 B 90 5A Z 114 72 r
43 2B 67 43 C 91 5B [ 115 73 s
44 2C 68 44 D 92 5C \ 116 74 t
45 2D 69 45 E 93 5D ] 117 75 u
46 2E 70 46 F 94 5E ^ 118 76 v
47 2F 71 47 G 95 5F _ 119 77 w
48 30 0 72 48 H 96 60 ` | 120 | 78 | x
49 31 1 73 49 I 97 61 a 121 79 y
50 32 2 74 4A J 98 62 b 122 7A z
51 33 3 75 4B K 99 63 c 123 7B
52 34 4 76 4C L 100 64 d 124 7C
53 35 5 77 4D M 101 65 e 125 7D
54 36 6 78 4E N 102 66 f 126 7E
55 37 7 79 4F O 103 67 g
分享:
扫描分享到社交APP
上一篇
下一篇