好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java的Character类详解

在实际开发过程中,我们经常会遇到需要使用对象,而不是内置数据类型的情况。为了解决这个问题,Java语言为内置数据类型char提供了包装类Character类。 Character 类用于对单个字符进行操作。Character 类在对象中包装一个基本类型 char 的值 。Character类提供了一系列方法来操纵字符。你可以使用Character的构造方法创建一个Character类对象,例如:

?

1

Character ch = new Character( 'a' );

在某些情况下,Java编译器会自动创建一个Character对象。例如,将一个char类型的参数传递给需要一个Character类型参数的方法时,那么编译器会自动地将char类型参数转换为Character对象。 这种特征称为装箱,反过来称为拆箱。

转义序列

前面有反斜杠(\)的字符代表转义字符,它对编译器来说是有特殊含义的 。下面列表展示了Java的转义序列:

转义序列 描述
\t 在文中该处插入一个tab键
\b 在文中该处插入一个后退键
\n 在文中该处换行
\n 在文中该处换行
\f 在文中该处插入换页符
' 在文中该处插入单引号
" 在文中该处插入双引号
\\ 在文中该处插入反斜杠

Character 方法

下面是Character类的类方法

序号 方法与描述
1 isLetter(),是否是一个字母
2 isDigit(),是否是一个数字字符
3 isWhitespace(),是否是一个空白字符
4 isUpperCase(),是否是大写字母
5 isLowerCase(),是否是小写字母
6 toUpperCase(),指定字母的大写形式
7 toLowerCase(),指定字母的小写形式
8 toString(),返回字符的字符串形式,字符串的长度仅为1

方法实例:

isLetter() 方法实例 :

?

1

public static boolean isLetter( char ch)

描述:

isLetter() 方法用于判断指定字符是否为字母。

参数:

ch -- 要测试的字符。

返回值:

如果字符为字母,则返回 true;否则返回 false。

?

1

2

3

4

5

6

7

8

9

10

public class Test {

     public static void main(String[] args) {

         System.out.println(Character.isLetter( 'c' ));

         System.out.println(Character.isLetter( '5' ));

     }

}

 

// 程序运行结果如下:

// true

// false

isDigit() 方法实例 :

?

1

public static boolean isDigit( char ch)

描述:

isDigit() 方法用于判断指定字符是否为数字。

参数:

ch -- 要测试的字符。

返回值:

如果字符为数字,则返回 true;否则返回 false。

?

1

2

3

4

5

6

7

8

9

10

public class Test {

     public static void main(String[] args) {

         System.out.println(Character.isDigit( 'c' ));

         System.out.println(Character.isDigit( '5' ));

     }

}

 

// 程序运行结果如下:

// false

// true

isWhitespace() 方法实例 :

?

1

public static boolean isWhitespace( char ch)

描述:

isWhitespace() 方法用于判断指定字符是否为空白字符,空白符包含:空格、tab 键、换行符、回车符和换页符。

参数:

ch -- 要测试的字符。

返回值:

如果字符为空白字符,则返回 true;否则返回 false。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

public class Test {

     public static void main(String[] args) {

         System.out.println(Character.isWhitespace( 'c' ));

         System.out.println(Character.isWhitespace( ' ' ));

         System.out.println(Character.isWhitespace( '\n' ));

         System.out.println(Character.isWhitespace( '\t' ));

         System.out.println(Character.isWhitespace( '\f' ));

         System.out.println(Character.isWhitespace( '\r' ));

     }

}

 

// 程序运行结果如下:

// false

// true

// true

// true

// true

// true

isUpperCase() 方法实例 :

?

1

public static boolean isUpperCase( char ch)

描述:

isUpperCase() 方法用于判断指定字符是否为大写字母。

参数:

ch -- 要测试的字符。

返回值:

如果字符为大写,则返回 true;否则返回 false。

?

1

2

3

4

5

6

7

8

9

10

public class Test {

     public static void main(String[] args) {

         System.out.println( Character.isUpperCase( 'c' ));

         System.out.println( Character.isUpperCase( 'C' ));

     }

}

 

// 程序运行结果如下:

// false

// true

isLowerCase() 方法实例 :

?

1

public static boolean isLowerCase( char ch)

描述:

isLowerCase() 方法用于判断指定字符是否为小写字母。

参数:

ch -- 要测试的字符。

返回值:

如果字符为小写,则返回 true;否则返回 false。

?

1

2

3

4

5

6

7

8

9

10

public class Test {

     public static void main(String[] args) {

         System.out.println( Character.isLowerCase( 'c' ));

         System.out.println( Character.isLowerCase( 'C' ));

     }

}

 

// 程序运行结果如下:

// true

// false

toUpperCase() 方法实例 :

?

1

public static char toUpperCase( char ch)

描述:

toUpperCase() 方法用于将小写字符转换为大写。

参数:

ch -- 要转换的字符。

返回值:

如果有的话,返回转换后字符的大写形式;否则返回字符本身。

?

1

2

3

4

5

6

7

8

9

10

public class Test {

     public static void main(String[] args) {

         System.out.println(Character.toUpperCase( 'a' ));

         System.out.println(Character.toUpperCase( 'A' ));

     }

}

 

// 程序运行结果如下:

// A

// A

toLowerCase() 方法实例 :

?

1

public static char toLowerCase( char ch)

描述:

toLowerCase() 方法用于将大写字符转换为小写。

参数:

ch -- 要转换的字符。

返回值:

如果有的话,返回转换后字符的小写形式;否则返回字符本身。

?

1

2

3

4

5

6

7

8

9

10

public class Test {

     public static void main(String[] args) {

         System.out.println(Character.toLowerCase( 'a' ));

         System.out.println(Character.toLowerCase( 'A' ));

     }

}

 

// 程序运行结果如下:

// a

// a

toString() 方法实例 :

?

1

public static String toString( char c)

描述:

toString() 方法用于返回一个表示指定 char 值的 String 对象。结果是长度为 1 的字符串,仅由指定的 char 组成。

参数:

ch -- 要转换的字符。

返回值:

返回指定 char 值的字符串表示形式。

?

1

2

3

4

5

6

7

8

9

10

public class Test {

     public static void main(String[] args) {

         System.out.println(Character.toString( 'a' ));

         System.out.println(Character.toString( 'A' ));

     }

}

 

// 程序运行结果如下:

// a

// A

到此这篇关于Java的Character类详解的文章就介绍到这了,更多相关Java Character类内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://juejin.cn/post/7224506104356995131

查看更多关于Java的Character类详解的详细内容...

  阅读:32次