Showing posts with label whitespace. Show all posts
Showing posts with label whitespace. Show all posts

Saturday, 12 November 2022

Check whether a string has any non-white space character or not

Write a function that takes a string as input and return true, if the string contain any non-whitespace character, else false.

 

A character is a Java whitespace character if and only if it satisfies one of the following criteria:

a.   It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').It is '\t', U+0009 HORIZONTAL TABULATION.

b.   It is '\n', U+000A LINE FEED.

c.    It is '\u000B', U+000B VERTICAL TABULATION.

d.   It is '\f', U+000C FORM FEED.

e.   It is '\r', U+000D CARRIAGE RETURN.

f.     It is '\u001C', U+001C FILE SEPARATOR.

g.   It is '\u001D', U+001D GROUP SEPARATOR.

h.   It is '\u001E', U+001E RECORD SEPARATOR.

i.     It is '\u001F', U+001F UNIT SEPARATOR.

 

Signature

public static boolean hasNonWhitespaceChar(final String str)

 


Find the below working application.

 

StringNonWhitspaceCharCheck.java
package com.sample.app.strings;

public class StringNonWhitspaceCharCheck {

	/**
	 * 
	 * @param str
	 * 
	 * @return true if str contains any non whitespace characters, else false.
	 */
	public static boolean hasNonWhitespaceChar(final String str) {
		if (str == null || str.isEmpty()) {
			return false;
		}

		final int length = str.length();
		for (int i = 0; i < length; i++) {
			if (Character.isWhitespace(str.charAt(i))) {
				continue;
			}

			return true;
		}
		return false;
	}

	public static void main(String[] args) {
		String str1 = "\t  \n\r";
		String str2 = "\t a  \n";
		
		System.out.println("is str1 contain any non whitespace character : "+ hasNonWhitespaceChar(str1));
		System.out.println("is str2 contain any non whitespace character : "+ hasNonWhitespaceChar(str2));
	}
}

 

Output

is str1 contain any non whitespace character : false
is str2 contain any non whitespace character : true

 

References

https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html

 


 

Previous                                                 Next                                                 Home

Tuesday, 7 December 2021

Java: How to check whether string contain only whitespaces or not?

What is whitespace in Java?

A character is a Java whitespace character if and only if it satisfies one of the following criteria:

 

a.   It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').

b.   It is '\t', U+0009 HORIZONTAL TABULATION.

c.    It is '\n', U+000A LINE FEED.

d.   It is '\u000B', U+000B VERTICAL TABULATION.

e.   It is '\f', U+000C FORM FEED.

f.     It is '\r', U+000D CARRIAGE RETURN.

g.   It is '\u001C', U+001C FILE SEPARATOR.

h.   It is '\u001D', U+001D GROUP SEPARATOR.

i.     It is '\u001E', U+001E RECORD SEPARATOR.

j.     It is '\u001F', U+001F UNIT SEPARATOR.

 

There are multiple approaches to find whether given string contain only whitespaces or not.

 

Approach 1: Using ‘str.trim().isEmpty()’ method or ‘str.strip().isEmpty()’

 

StringContainWhiteSpacesDemo1.java

package com.sample.app.strings;

public class StringContainWhiteSpacesDemo1 {

	private static boolean containOnlyWhiteSpaces(String str) {
		if (str == null) {
			return false;
		}

		return str.trim().isEmpty();
	}

	public static void main(String args[]) {
		String str1 = "Hello";
		String str2 = "\n\t\f\r   \n\t";

		System.out.println("is str1 contains only whitespaces : " + containOnlyWhiteSpaces(str1));
		System.out.println("is str2 contains only whitespaces : " + containOnlyWhiteSpaces(str2));
	}

}

 

But there is a limitation in using trim() method. String::trim has existed from early days of Java when Unicode had not fully evolved to the standard we widely use today.

 

The definition of space used by String::trim is any code point less than or equal to the space code point (\u0020), commonly referred to as ASCII or ISO control characters.

 

Java11 defined strip() api to address this problem. strip() is "Unicode-aware" evolution of trim().

 

StringContainWhiteSpacesDemo1.java

public class StringContainWhiteSpacesDemo1 {

	private static boolean containOnlyWhiteSpaces(String str) {
		if (str == null) {
			return false;
		}

		return str.strip().isEmpty();
	}

	public static void main(String args[]) {
		String str1 = "Hello";
		String str2 = "\n\t\f\r   \n\t";

		System.out.println("is str1 contains only whitespaces : " + containOnlyWhiteSpaces(str1));
		System.out.println("is str2 contains only whitespaces : " + containOnlyWhiteSpaces(str2));
	}

}

 

Approach 2: Using Character.isWhitespace method.

public static boolean isWhitespace(char ch)

public static boolean isWhitespace(int codePoint)

'isWhitespace' method return true if the character is a Java whitespace character, false otherwise. 'isWhitespace(char ch)'. isWhitespace(char ch) method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the isWhitespace(int) method.

 

StringContainWhiteSpacesDemo2.java

package com.sample.app.strings;

public class StringContainWhiteSpacesDemo2 {

	private static boolean containOnlyWhiteSpaces(String str) {
		if (str == null) {
			return false;
		}

		char[] chars = str.toCharArray();

		for (Character ch : chars) {
			if (!Character.isWhitespace((int) ch)) {
				return false;
			}
		}

		return true;
	}

	public static void main(String args[]) {
		String str1 = "Hello";
		String str2 = "\n\t\f\r   \n\t";

		System.out.println("is str1 contains only whitespaces : " + containOnlyWhiteSpaces(str1));
		System.out.println("is str2 contains only whitespaces : " + containOnlyWhiteSpaces(str2));
	}

}

 

Approach 3: Using String#isBlank() method

isBlank() method is introduced in Java11. This method return true, if the string is empty or contains only white space codepoints, otherwise false.

 

StringContainWhiteSpacesDemo3.java

 

public class StringContainWhiteSpacesDemo3 {

	private static boolean containOnlyWhiteSpaces(String str) {
		if (str == null) {
			return false;
		}

		return str.isBlank();
	}

	public static void main(String args[]) {
		String str1 = "Hello";
		String str2 = "\n\t\f\r   \n\t";

		System.out.println("is str1 contains only whitespaces : " + containOnlyWhiteSpaces(str1));
		System.out.println("is str2 contains only whitespaces : " + containOnlyWhiteSpaces(str2));
	}

}




References

https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isWhitespace(char)

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#isBlank()

https://bugs.openjdk.java.net/browse/JDK-8200378




 

 

You may like

Interview Questions

How to check the type of a variable in Java?

Set a system property in command line

How to get the capacity of ArrayList in Java?

volatile reference vs Atomic references defined in java.util.concurrent.atomic package

Check whether I have JDK or JRE in my system

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