Showing posts with label Character. Show all posts
Showing posts with label Character. 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

Wednesday, 29 April 2020

Check whether given character present in string or not

Using ‘indexOf’ method, we can check whether given character is present in string or not. ‘indexOf()’ method return -1, if given character is not present in the string, else it return the index of the first occurrence of the character in the string.

Example
private static boolean isCharExist(String str, char ch) {
         if (str == null) {
                  throw new IllegalArgumentException("String can't be null");
         }

         return str.indexOf(ch) != -1;
}

Find the following working application.

App.java
package com.sample.app;

public class App {

 private static boolean isCharExist(String str, char ch) {
  if (str == null) {
   throw new IllegalArgumentException("String can't be null");
  }

  return str.indexOf(ch) != -1;
 }

 public static void main(String args[]) {
  String str = "Hello World";

  char ch = 'H';
  System.out.println("is " + ch + " exists in " + str + " : " + isCharExist(str, ch));

  ch = 'o';
  System.out.println("is " + ch + " exists in " + str + " : " + isCharExist(str, ch));

  ch = 'a';
  System.out.println("is " + ch + " exists in " + str + " : " + isCharExist(str, ch));
 }

}

Output
is H exists in Hello World : true
is o exists in Hello World : true
is a exists in Hello World : false


You may like

Tuesday, 21 April 2020

Java: Convert String to a character

Following snippet converts the string to a character.

public static char getChar(String str) {
         if (str == null || str.length() > 1) {
                  throw new IllegalArgumentException("String must not be null and has length 1");
         }

         return str.charAt(0);
}

App.java
package com.sample.app;

public class App {

	public static char getChar(String str) {
		if (str == null || str.length() > 1) {
			throw new IllegalArgumentException("String must not be null and has length 1");
		}

		return str.charAt(0);
	}

	public static void main(String args[]) {

		char ch = getChar("A");

		System.out.println("ch : " + ch);
	}

}

Output
ch : A

You may like

Monday, 2 March 2020

Java: Check given character is letter or not

'Character.isLetter' method is used to check whether given character is letter or not.

App.java
package com.sample.app;

public class App {

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

Output

'a' is a letter
'@' is not a letter
'	' is not a letter
' ' is not a letter
',' is not a letter


You may like

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