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


Java Character.isWhitespace()用法及代码示例


java.lang.Character.isWhitespace()是Java中的一种内置方法,该方法根据Java确定指定字符(Unicode代码点)是否为空格。当且仅当满足以下条件之一时,字符才是Java空白字符:

  • 它是Unicode空格字符(SPACE_SEPARATOR,LINE_SEPARATOR或PARAGRAPH_SEPARATOR),但也不是不间断空格(“ \ u00A0”,“ \ u2007”,“ \ u202F”)。
  • U + 0009水平制表符是“ \ t”。
  • 它是“ \ n”,U + 000A LINE FEED。
  • 它是“ \ u000B”,U + 000B垂直制表符。
  • U + 000C FORM FEED是“ \ f”。
  • 是'\ r',U + 000D回车。
  • 它是“ \ u001C”,U + 001C文件分隔符。
  • 它是U \ 001D组分隔符“ \ u001D”。
  • 它是“ \ u001E”,U + 001E记录分隔符。
  • 它是“ \ u001F”,U + 001F单位分隔符。

用法:

public static boolean isWhitespace(datatype character)

参数:该函数接受一个强制性参数字符。此参数可以是int或char数据类型。它指定要测试的字符。


返回值:如果该字符是Java空白字符,则此方法返回true,否则返回false。

以下示例程序旨在说明Character.isWhitespace(char ch)方法:

示例1:

// Java program to illustrate the Character.isWhitespace() 
// method when the passed parameter is a character 
import java.lang.*; 
  
public class gfg { 
  
    public static void main(String[] args) 
    { 
  
        // create 2 char primitives c1, c2 
        char c1 = '*', c2 = '\f'; 
  
        boolean b1 = Character.isWhitespace(c1); 
        boolean b2 = Character.isWhitespace(c2); 
  
        String str1 = "c1 is a Java whitespace character is " + b1; 
        String str2 = "c2 is a Java whitespace character is " + b2; 
  
        // print b1, b2 values 
        System.out.println(str1); 
        System.out.println(str2); 
    } 
}

输出

c1 is a Java whitespace character is false
c2 is a Java whitespace character is true

示例2:

// Java program to demonstrate the Character.isWhitespace() 
// method when the passed parameter is a character 
import java.lang.*; 
  
public class gfg { 
  
    public static void main(String[] args) 
    { 
  
        // create 2 char primitives c1, c2 
        char c1 = '/', c2 = '\f'; 
  
        boolean b1 = Character.isWhitespace(c1); 
        boolean b2 = Character.isWhitespace(c2); 
  
        String str1 = "c1 is a Java whitespace character is " + b1; 
        String str2 = "c2 is a Java whitespace character is " + b2; 
  
        // print b1, b2 values 
        System.out.println(str1); 
        System.out.println(str2); 
    } 
}

输出

c1 is a Java whitespace character is false
c2 is a Java whitespace character is true

示例3:参数为int类型时。

// Java program to demonstrate the 
// Character.isWhitespace() 
// method when the passed parameter 
// is a character 
import java.lang.*; 
  
public class gfg { 
  
    public static void main(String[] args) 
    { 
  
        // create 2 int primitives c1, c2 
        int c1 = 0x451c, c2 = 0x4abc; 
  
        boolean b1 = Character.isWhitespace(c1); 
        boolean b2 = Character.isWhitespace(c2); 
  
        String str1 = "c1 represents Java whitespace character is " + b1; 
        String str2 = "c2 represents Java whitespace character is " + b2; 
  
        // print b1, b2 values 
        System.out.println(str1); 
        System.out.println(str2); 
    } 
}

输出

c1 represents Java whitespace character is false
c2 represents Java whitespace character is false

示例4:参数为int类型时。

// Java program to demonstrate the Character.isWhitespace() 
// method when the passed parameter is a character 
import java.lang.*; 
  
public class gfg { 
  
    public static void main(String[] args) 
    { 
  
        // create 2 int primitives c1, c2 
        int c1 = 0x001c, c2 = 0x1bbc; 
  
        boolean b1 = Character.isWhitespace(c1); 
        boolean b2 = Character.isWhitespace(c2); 
  
        String str1 = "c1 represents Java whitespace character is " + b1; 
        String str2 = "c2 represents Java whitespace character is " + b2; 
  
        // print b1, b2 values 
        System.out.println(str1); 
        System.out.println(str2); 
    } 
}

输出

c1 represents Java whitespace character is true
c2 represents Java whitespace character is false


相关用法


注:本文由纯净天空筛选整理自Twinkl Bajaj大神的英文原创作品 Character.isWhitespace() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。