當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。