Java - Character isSpaceChar() Method



Description

The Java Character isSpaceChar() method determines if the specified character is a Unicode space character. A character is considered to be a space character if and only if it is specified to be a space character by the Unicode Standard.

This method returns true if the character's general category type is any of the following −

  • SPACE_SEPARATOR

  • LINE_SEPARATOR

  • PARAGRAPH_SEPARATOR

Syntax

Following is the syntax for Java Character isSpaceChar() method

public static boolean isSpaceChar(char ch)
(or)
public static boolean isSpaceChar(int codePoint)

Parameters

  • ch − the character to be tested

  • codePoint − the Unicode code point to be tested

Return Value

This method returns true if the character is a space character, false otherwise.

Checking if a char is a space char Example

The following example shows the usage of Java Character isSpaceChar(char ch) method. We've initialized two char variables with two char values. Using isSpaceChar() method, we're checking if the char variables contains space char or not. Then result is printed.

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 char primitives ch1, ch2
      char ch1, ch2;

      // assign values to ch1, ch2
      ch1 = ' ';
      ch2 = '\u2028';  // LINE_SEPARATOR

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      /**
       *  check if ch1, ch2 are space characters
       *  and assign results to b1, b2
       */
      b1 = Character.isSpaceChar(ch1);
      b2 = Character.isSpaceChar(ch2);
      String str1 = "ch1 is a space character is " + b1;
      String str2 = "ch2 is a space character is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

ch1 is a space character is true
ch2 is a space character is true

Checking if a codepoint is a space char Example

The following example shows the usage of Java Character isSpaceChar(int codepoint) method. We've initialized two int variables with two codepoint values. Using isSpaceChar() method, we're checking if the int variables contains space char or not. Then result is printed.

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 int primitives cp1, cp2
      int cp1, cp2;

      // assign values to cp1, cp2
      cp1 = 0x2029; // PARAGRAPH_SEPARATOR
      cp2 = 0x1010;

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      /**
       *  check if cp1, cp2 represent space characters
       *  and assign results to b1, b2
       */
      b1 = Character.isSpaceChar(cp1);
      b2 = Character.isSpaceChar(cp2);
      String str1 = "cp1 represents a space character is " + b1;
      String str2 = "cp2 represents a space character is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

cp1 represents a space character is true
cp2 represents a space character is false

Checking if a string contains a space char Example

The example below demonstrates how to check whether a character sequence, or a string, contains a space character. This is done by using loop and conditional statements.

package com.tutorialspoint;

public class SpaceCharDemo {
   public static void main(String []args){
      String s = "Hello World";
      int count = 0;
      for(int i = 0; i < s.length(); i++) {
         char ch = s.charAt(i);
         if(Character.isSpaceChar(ch)) {
            System.out.println("The Space char is found at index " + i);
            count = count + 1;
         }
      }
      if(count == 0)
         System.out.println("The Space char is not found");
   }
}

Output

Once the program above is compiled and run, the output will be printed as −

The Space char is found at index 5

Checking if a string with line separator contains a space char Example

In another example, we will input a character sequence including a code point of the line separator and using this method, we find at which index the line separator is at.

package com.tutorialspoint;

public class SpaceCharDemo {
   public static void main(String []args){
      String s = "Tutorials" + '\u2028' + "point";
      for(int i = 0; i < s.length(); i++) {
         char ch = s.charAt(i);
         if(Character.isSpaceChar((int)ch)) {
            System.out.println("The Space char is found at index " + i);
         }
      }
   }
}

Output

Once the program is compiled and run, the output will be obtained as follows −

The Space char is found at index 9
java_lang_character.htm
Advertisements