当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Character isSpace()用法及代码示例


Character 类的 isSpace(char ch) 方法确定给定(或指定)字符是否为 ISO-LATIN-1 空格。

给定的方法对以下五个字符返回 true:

  • '\t' U+0009 水平制表
  • '\n' U+000A 新线
  • '\f' U+000C 表格进纸
  • '\r' U+000D 回车
  • ' ' U+0020 空格

用法

public static boolean isSpace(char ch)

参数

ch: 这是需要测试的字符。

返回值

isSpace(char ch) 方法返回一个布尔值,即如果给定(或指定)字符是 ISO-LATIN-1 空格,则返回 true。否则,该方法返回 false。

例子1

public class JavaCharacterIsSapceExample1 {
  public static void main(String args[])
  {
      //Create three char primitives.
       char ch1 = '\n';
       char ch2 = '\t';
       char ch3 = ' ';
     //Create three boolean primitives.
       boolean b1 = Character.isSpace(ch1);
       boolean b2 = Character.isSpace(ch2);
        boolean b3 = Character.isSpace(ch3);
     // Print the result.
      System.out.println("The first character is a space character:" + b1);     
      System.out.println("The second character is a space character:" + b2);     
      System.out.println("The third character is a space character:" + b3);     
     }
}

输出:

The first character is a space character:true
The second character is a space character:true
The third character is a space character:true

例子2

public class JavaCharacterIsSapceExample2 {
      public static void main(String args[])
  {
      //Create two char primitives.
       char ch1 = '\f';
       char ch2 = '\r';
  
     //Create two boolean primitives.
       boolean b1 = Character.isSpace(ch1);
       boolean b2 = Character.isSpace(ch2);
     // Print the result.
      System.out.println("The first character is a space character:" + b1);     
      System.out.println("The second character is a space character:" + b2);    
     }
}

输出:

The first character is a space character:true
The second character is a space character:true




相关用法


注:本文由纯净天空筛选整理自 Java Character isSpace() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。