Monday, 2 March 2020

Java: Check whether given character is whitespace or not

'Character.isWhitespace' method takes a character and return true if the given character is whitespace, else false.

App.java
package com.sample.app;

public class App {

	private static void printWhitSpace(char charz) {
		if(Character.isWhitespace(charz)) {
			System.out.println("'" + charz + "' is a whitespace");
		}else {
			System.out.println("'" + charz + "' is not a whitespace");
		}
	}
	
	public static void main(String args[]) {
	
		printWhitSpace('a');
		printWhitSpace('\t');
		printWhitSpace(' ');
		printWhitSpace('@');
		
	}
}

Output
'a' is not a whitespace
'	' is a whitespace
' ' is a whitespace
'@' is not a whitespace


You may like

No comments:

Post a Comment