📜  Java中的字符类

📅  最后修改于: 2022-05-13 01:55:45.774000             🧑  作者: Mango

Java中的字符类

Java在Java .lang 包中提供了一个包装类字符 。 字符类型的对象包含一个字段,其类型为 char。 字符类提供了许多有用的类(即静态)方法来操作字符。您可以使用字符构造函数创建字符对象。

创建一个字符对象:

Character ch = new Character('a');

上面的语句创建了一个字符对象,其中包含 char 类型的“a”。 字符类中只有一个构造函数需要 char 数据类型的参数。

如果我们将原始 char 传递给需要对象的方法,编译器会自动将 char 转换为字符类对象。此功能称为自动装箱和拆箱。

字符类中的方法

字符类的方法如下:

1. boolean isLetter(char ch):该方法用于判断指定的char value(ch)是否为字母。如果是 letter([AZ],[az]),该方法将返回 true,否则返回 false。代替字符,我们还可以将 ASCII 值作为参数传递,因为 char 到 int 在Java中是隐式类型转换的。

句法:

boolean isLetter(char ch)

参数:

  • ch –原始字符

返回:如果 ch 是字母则返回 true,否则返回 false

例子:

Java
// Java program to demonstrate isLetter() method
  
public class Test {
    public static void main(String[] args)
    {
        System.out.println(Character.isLetter('A'));
  
        System.out.println(Character.isLetter('0'));
    }
}


Java
// Java program to demonstrate isDigit() method
  
public class Test {
    public static void main(String[] args)
    {
        // print false as A is character
        System.out.println(Character.isDigit('A'));
  
        System.out.println(Character.isDigit('0'));
    }
}


Java
// Java program to demonstrate isWhitespace() method
  
public class Test {
    public static void main(String[] args)
    {
        System.out.println(Character.isWhitespace('A'));
        System.out.println(Character.isWhitespace(' '));
        System.out.println(Character.isWhitespace('\n'));
        System.out.println(Character.isWhitespace('\t'));
  
        // ASCII value of tab
        System.out.println(Character.isWhitespace(9));
  
        System.out.println(Character.isWhitespace('9'));
    }
}


Java
// Java program to demonstrate isUpperCase() method
  
public class Test {
    public static void main(String[] args)
    {
        System.out.println(Character.isUpperCase('A'));
        System.out.println(Character.isUpperCase('a'));
        System.out.println(Character.isUpperCase(65));
    }
}


Java
// Java program to demonstrate isLowerCase() method
  
public class Test {
    public static void main(String[] args)
    {
        System.out.println(Character.isLowerCase('A'));
        System.out.println(Character.isLowerCase('a'));
        System.out.println(Character.isLowerCase(97));
    }
}


Java
// Java program to demonstrate toUpperCase() method
  
public class Test {
    public static void main(String[] args)
    {
        System.out.println(Character.toUpperCase('a'));
        System.out.println(Character.toUpperCase(97));
        System.out.println(Character.toUpperCase(48));
    }
}


Java
// Java program to demonstrate toLowerCase() method
  
public class Test {
    public static void main(String[] args)
    {
        System.out.println(Character.toLowerCase('A'));
        System.out.println(Character.toLowerCase(65));
        System.out.println(Character.toLowerCase(48));
    }
}


Java
// Java program to demonstrate toString() method
  
public class Test {
    public static void main(String[] args)
    {
        System.out.println(Character.toString('x'));
        System.out.println(Character.toString('Y'));
    }
}


输出
true
false

2. boolean isDigit(char ch) 该方法用于判断指定的char value(ch)是否为数字。在这里,我们也可以将 ASCII 值作为参数传递。

句法:

boolean isDigit(char ch)

参数:

  • ch –原始字符

返回:如果 ch 是数字则返回 true,否则返回 false

例子:

Java

// Java program to demonstrate isDigit() method
  
public class Test {
    public static void main(String[] args)
    {
        // print false as A is character
        System.out.println(Character.isDigit('A'));
  
        System.out.println(Character.isDigit('0'));
    }
}
输出
false
true

3. boolean isWhitespace(char ch ):判断指定的char值(ch)是否为空白。空白包括空格、制表符或换行符。

句法:

boolean isWhitespace(char ch)

参数:

  • ch –原始字符

返回:如果 ch 是空格,则返回 true,否则返回 false。

例子:

Java

// Java program to demonstrate isWhitespace() method
  
public class Test {
    public static void main(String[] args)
    {
        System.out.println(Character.isWhitespace('A'));
        System.out.println(Character.isWhitespace(' '));
        System.out.println(Character.isWhitespace('\n'));
        System.out.println(Character.isWhitespace('\t'));
  
        // ASCII value of tab
        System.out.println(Character.isWhitespace(9));
  
        System.out.println(Character.isWhitespace('9'));
    }
}
输出
false
true
true
true
true
false

4. boolean isUpperCase(char ch):判断指定的char值(ch)是否为大写。

句法:

boolean isUpperCase(char ch)

参数:

  • ch –原始字符

返回:如果 ch 为大写,则返回 true,否则返回 false。

例子:

Java

// Java program to demonstrate isUpperCase() method
  
public class Test {
    public static void main(String[] args)
    {
        System.out.println(Character.isUpperCase('A'));
        System.out.println(Character.isUpperCase('a'));
        System.out.println(Character.isUpperCase(65));
    }
}
输出
true
false
true

5. boolean isLowerCase(char ch):判断指定的char值(ch)是否为小写。

句法:

boolean isLowerCase(char ch)

参数:

  • ch –原始字符

返回:如果 ch 为小写则返回 true,否则返回 false。

例子:

Java

// Java program to demonstrate isLowerCase() method
  
public class Test {
    public static void main(String[] args)
    {
        System.out.println(Character.isLowerCase('A'));
        System.out.println(Character.isLowerCase('a'));
        System.out.println(Character.isLowerCase(97));
    }
}
输出
false
true
true

6. char toUpperCase(char ch):返回指定char值(ch)的大写。如果传递了 ASCII 值,则返回其大写的 ASCII 值。

句法:

char toUpperCase(char ch)

参数:

  • ch –原始字符

返回:它返回指定 char 值的大写形式。

例子:

Java

// Java program to demonstrate toUpperCase() method
  
public class Test {
    public static void main(String[] args)
    {
        System.out.println(Character.toUpperCase('a'));
        System.out.println(Character.toUpperCase(97));
        System.out.println(Character.toUpperCase(48));
    }
}
输出
A
65
48

7. char toLowerCase(char ch):返回指定char值(ch)的小写字母。

句法:

char toLowerCase(char ch)

参数:

  • ch –原始字符

返回:它返回指定 char 值的小写形式。

例子:

Java

// Java program to demonstrate toLowerCase() method
  
public class Test {
    public static void main(String[] args)
    {
        System.out.println(Character.toLowerCase('A'));
        System.out.println(Character.toLowerCase(65));
        System.out.println(Character.toLowerCase(48));
    }
}
输出
a
97
48

8. toString(char ch):返回一个String类对象,表示指定的字符值(ch),即一个字符的字符串。这里我们不能传递 ASCII 值。

句法:

String toString(char ch)

参数:

  • ch –原始字符

返回:它返回一个字符串对象。

例子:

Java

// Java program to demonstrate toString() method
  
public class Test {
    public static void main(String[] args)
    {
        System.out.println(Character.toString('x'));
        System.out.println(Character.toString('Y'));
    }
}
输出
x
Y

Java中字符类的方法

S. No.MethodDescription
1.static int charCount​(int codePoint)This method determines the number of char values needed to represent the specified character (Unicode code point).
2.char charValue​()This method returns the value of this Character object.
3.static int codePointAt​(char[] a, int index)This method returns the code point at the given index of the char array.
4.static int codePointAt​(char[] a, int index, int limit)This method returns the code point at the given index of the char array, where only array elements with an index less than the limit can be used.
5.static int codePointAt​(CharSequence seq, int index)This method returns the code point at the given index of the CharSequence.
6.static int codePointBefore​(char[] a, int index)This method returns the code point preceding the given index of the char array.
7.static int codePointBefore​(char[] a, int index, int start)This method returns the code point preceding the given index of the char array, where only array elements with index greater than or equal to start can be used.
8.static int codePointBefore​(CharSequence seq, int index)This method returns the code point preceding the given index of the CharSequence.
9.static int codePointCount​(char[] a, int offset, int count)This method returns the number of Unicode code points in a subarray of the char array argument.
10.static int codePointCount​(CharSequence seq, int beginIndex, int endIndex)This method returns the number of Unicode code points in the text range of the specified char sequence.
11.static int codePointOf​(String name)This method returns the code point value of the Unicode character specified by the given Unicode character name.
12.static int compare​(char x, char y)This method compares two char values numerically.
13.int compareTo​(Character anotherCharacter)This method compares two Character objects numerically.
14.static int digit​(char ch, int radix)This method returns the numeric value of the character ch in the specified radix.
15.static int digit​(int codePoint, int radix)This method returns the numeric value of the specified character (Unicode code point) in the specified radix.
16.boolean equals​(Object obj)This method compares this object against the specified object.
17.static char forDigit​(int digit, int radix)This method determines the character representation for a specific digit in the specified radix.
18.static byte getDirectionality​(char ch)This method returns the Unicode directionality property for the given character.
19.static byte getDirectionality​(int codePoint)This method returns the Unicode directionality property for the given character (Unicode code point).
20.static String getName​(int codePoint)This method returns the Unicode name of the specified character codePoint, or null if the code point is unassigned.
21.static int getNumericValue​(char ch)This method returns the int value that the specified Unicode character represents.
22.static int getNumericValue​(int codePoint)This method returns the int value that the specified character (Unicode code point) represents.
23.static int getType​(char ch)This method returns a value indicating a character’s general category.
24.static int getType​(int codePoint)This method returns a value indicating a character’s general category.
25.int hashCode​()This method returns a hash code for this Character; equal to the result of invoking charValue().
26.static int hashCode​(char value)This method returns a hash code for a char value; compatible with Character.hashCode().
27.static char highSurrogate​(int codePoint)This method returns the leading surrogate (a high surrogate code unit) of the surrogate pair representing the specified supplementary character (Unicode code point) in the UTF-16 encoding.
28.static boolean isAlphabetic​(int codePoint)This method determines if the specified character (Unicode code point) is an alphabet.
29.static boolean isBmpCodePoint​(int codePoint)This method determines whether the specified character (Unicode code point) is in the Basic Multilingual Plane (BMP).
30.static boolean isDefined​(char ch)This method determines if a character is defined in Unicode.
31.static boolean isDefined​(int codePoint)This method determines if a character (Unicode code point) is defined in Unicode.
32.static boolean isHighSurrogate​(char ch)This method determines if the given char value is a Unicode high-surrogate code unit (also known as a leading-surrogate code unit).
33.static boolean isIdentifierIgnorable​(char ch)This method determines if the specified character should be regarded as an ignorable character in a Java identifier or a Unicode identifier.
34.static boolean isIdentifierIgnorable​(int codePoint)This method determines if the specified character (Unicode code point) should be regarded as an ignorable character in a Java identifier or a Unicode identifier.
35.static boolean isIdeographic​(int codePoint)This method determines if the specified character (Unicode code point) is a CJKV (Chinese, Japanese, Korean, and Vietnamese) ideograph, as defined by the Unicode Standard.
36.static boolean isISOControl​(char ch)This method determines if the specified character is an ISO control character.
37.static boolean isISOControl​(int codePoint)This method determines if the referenced character (Unicode code point) is an ISO control character.
38.static boolean isJavaIdentifierPart​(char ch)This method determines if the specified character may be part of a Java identifier as other than the first character.
39.static boolean isJavaIdentifierPart​(int codePoint)This method determines if the character (Unicode code point) may be part of a Java identifier as other than the first character.
40.static boolean isJavaIdentifierStart​(char ch)This method determines if the specified character is permissible as the first character in a Java identifier.
41.static boolean isJavaIdentifierStart​(int codePoint)This method determines if the character (Unicode code point) is permissible as the first character in a Java identifier.
42.static boolean isLowSurrogate​(char ch)This method determines if the given char value is a Unicode low-surrogate code unit (also known as trailing-surrogate code unit).
43.static boolean isLetterOrDigit​(char ch)This method determines if the specified character is a letter or digit.
44.static boolean isLetterOrDigit​(int codePoint)This method determines if the specified character (Unicode code point) is a letter or digit.
45.static boolean isMirrored​(char ch)This method determines whether the character is mirrored according to the Unicode specification.
46.static boolean isMirrored​(int codePoint)This method determines whether the specified character (Unicode code point) is mirrored according to the Unicode specification.
47.static boolean isSpaceChar​(char ch)This method determines if the specified character is a Unicode space character.
48.static boolean isSpaceChar​(int codePoint)This method determines if the specified character (Unicode code point) is a Unicode space character.
49.static boolean isSupplementaryCodePoint​(int codePoint)This method determines whether the specified character (Unicode code point) is in the supplementary character range.
50.static boolean isSurrogate​(char ch)This method determines if the given char value is a Unicode surrogate code unit.
51.static boolean isSurrogatePair​(char high, char low)This method determines whether the specified pair of char values is a valid Unicode surrogate pair.
52.static boolean isTitleCase​(char ch)This method determines if the specified character is a titlecase character.
53.static boolean isTitleCase​(int codePoint)This method determines if the specified character (Unicode code point) is a titlecase character.
54.static boolean isUnicodeIdentifierPart​(char ch)This method determines if the specified character may be part of a Unicode identifier as other than the first character.
55.static boolean isUnicodeIdentifierPart​(int codePoint)This method determines if the specified character (Unicode code point) may be part of a Unicode identifier as other than the first character.
56.static boolean isUnicodeIdentifierStart​(char ch)This method determines if the specified character is permissible as the first character in a Unicode identifier.
57.static boolean isUnicodeIdentifierStart​(int codePoint)This method determines if the specified character (Unicode code point) is permissible as the first character in a Unicode identifier.
58.static boolean isValidCodePoint​(int codePoint)This method determines whether the specified code point is a valid Unicode code point value.
59.static char lowSurrogate​(int codePoint)This method returns the trailing surrogate (a low surrogate code unit) of the surrogate pair representing the specified supplementary character (Unicode code point) in the UTF-16 encoding.
60.static int offsetByCodePoints​(char[] a, int start, int count, int index, int codePointOffset)This method returns the index within the given char subarray that is offset from the given index by codePointOffset code points.
61.static int offsetByCodePoints​(CharSequence seq, int index, int codePointOffset)This method returns the index within the given char sequence that is offset from the given index by codePointOffset code points.
62.static char reverseBytes​(char ch)This method returns the value obtained by reversing the order of the bytes in the specified char value.
63.static char[] toChars​(int codePoint)This method converts the specified character (Unicode code point) to its UTF-16 representation stored in a char array.
64.static int toChars​(int codePoint, char[] dst, int dstIndex)This method converts the specified character (Unicode code point) to its UTF-16 representation.
65.static int toCodePoint​(char high, char low)This method converts the specified surrogate pair to its supplementary code point value.
66.static char toTitleCase​(char ch)This method converts the character argument to titlecase using case mapping information from the UnicodeData file.
67.static int toTitleCase​(int codePoint)This method converts the character (Unicode code point) argument to titlecase using case mapping information from the UnicodeData file.
68.static Character valueOf​(char c)This method returns a Character instance representing the specified char value.

转义序列:

以反斜杠 (\) 开头的字符是转义序列,对编译器具有特殊意义。下表显示了Java转义序列:

Escape SequenceDescription
\tInsert a tab in the text at this point.
\bInsert a backspace in the text at this point.
\nInsert a newline in the text at this point.
\rInsert a carriage return in the text at this point.
\fInsert a formfeed in the text at this point.
\’Insert a single quote character in the text at this point.
\”Insert a double quote character in the text at this point.
\\Insert a backslash character in the text at this point.

当在 print 语句中遇到转义序列时,编译器会相应地解释它。例如,如果要将引号放在引号内,则必须在内部引号上使用转义序列 \"。打印句子

She said "Hello!" to me.

你会写

System.out.println("She said \"Hello!\" to me.");