Java Character isSpace() Example

Many methods exist in java.lang.Character class used for different purposes. Some methods are useful for checking the character is a digit or letter or is a uppercase letter or lowercase letter etc. At the sametime, some methods can modify the case of the letters from uppercase to lowercase or vice versa.

Few more methods exist to check whether the character is fit to be a space character or not like the present one isSpace(char ch) and the next one isWhiteSpace(char ch).

Note: isSpace(char ch) is deprecated in favor of isWhitespace(char ch).

Following is the method signature as defined in java.lang.Character class.

  • public static boolean isSpace(char ch): Checks the specified character ch is a space character or not. Returns a boolean value of true for space characters like \t, \n, \f, \r and ' ' (space in between single quotes).
Following "Java Character isSpace() Example" illustrates.
public class CharacterFunctionDemo
{
  public static void main(String args[])
  {
   char ch1 = ' ';
   char ch2 = '\t';                      

   boolean b1 = Character.isSpace(ch1);
   boolean b2 = Character.isSpace(ch2);

   System.out.println("' ' is a space character: " + b1);     
   System.out.println("\\t is a space character: " + b2);     

   System.out.println("\\n is a space character: " + Character.isSpace('\n'));
   System.out.println("\\f is a space character : " + Character.isSpace('\f'));
   System.out.println("\\r is a space character: " + Character.isSpace('\r'));
  }
}


Java Character isSpace() Example
Output screenshot on Java Character isSpace() Example

There comes a similar method with the same functionality – isWhitespace(char).

Leave a Comment

Your email address will not be published.