[Java] Check if the character string is composed only of blanks (= Blank)

I was curious about the contents of the validator of javax.validation.constraints.NotBlank, so I checked it.

manner

A code that returns true if it is blank.

public boolean isBlank(CharSequence charSequence) {
    if ( charSequence == null ) {
        return true;
    }

    return charSequence.toString().trim().length() == 0;
}

Reference source

This code is borrowed from the implementation of ʻorg.hibernate.validator.internal.constraintvalidators.bv.NotBlankValidator`.

/*
 * Hibernate Validator, declare and validate application constraints
 *
 * License: Apache License, Version 2.0
 * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
 */
package org.hibernate.validator.internal.constraintvalidators.bv;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.constraints.NotBlank;

/**
 * Check that a character sequence is not {@code null} nor empty after removing any leading or trailing whitespace.
 *
 * @author Guillaume Smet
 */
public class NotBlankValidator implements ConstraintValidator<NotBlank, CharSequence> {

	/**
	 * Checks that the character sequence is not {@code null} nor empty after removing any leading or trailing
	 * whitespace.
	 *
	 * @param charSequence the character sequence to validate
	 * @param constraintValidatorContext context in which the constraint is evaluated
	 * @return returns {@code true} if the string is not {@code null} and the length of the trimmed
	 * {@code charSequence} is strictly superior to 0, {@code false} otherwise
	 */
	@Override
	public boolean isValid(CharSequence charSequence, ConstraintValidatorContext constraintValidatorContext) {
		if ( charSequence == null ) {
			return false;
		}

		return charSequence.toString().trim().length() > 0;
	}
}

Recommended Posts

[Java] Check if the character string is composed only of blanks (= Blank)
How to check for the contents of a java fixed-length string
Static function to check if the RGB error of BufferdImage is within the specified ratio in Java
[Java beginner] Conversion from character string to numerical value-What is the parseInt method of the Integer class? ~
Check the contents of the Java certificate store
Memo: [Java] Check the contents of the directory
[Java] Check the number of occurrences of characters
[Delete the first letter of the character string] Ruby
The order of Java method modifiers is fixed
Character string comparison: I was caught in the skill check problem of Paiza
[Java] Get the length of the surrogate pair string
[Java] The confusing part of String and StringBuilder
[Note] Java: Measures the speed of string concatenation
[Java] How to easily get the longest character string of ArrayList using stream
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
[Java] When putting a character string in the case of a switch statement, it is necessary to make it a constant expression
[Java] Cut out a part of the character string with Matcher and regular expression
The comparison of enums is ==, and equals is good [Java]
[Java] Check the JDK version of the built war file
Check the behavior of Java Intrinsic Locks with bpftrace
[Java] Handling of character strings (String class and StringBuilder class)
Replace only part of the URL host with java
I want to find out if the specified character string is supported by the target character code
Library to check if it is an IPv4 format string
Command to check the number and status of Java threads
Check the status of Java application without using monitoring tool
[Java] Try editing the elements of the Json string using the library
Java: The problem of which is faster, stream or loop
Change only one character from the String type string to uppercase
[Introduction to Java] Handling of character strings (String class, StringBuilder class)
[Java] I felt as a java beginner. "Why is the first letter of the String type capitalized?" I faced the String type.
Basics of character operation (java)
Java is the 5th day
When the character string passed to C ++ by JNA is garbled
The story of not knowing the behavior of String by passing Java by reference
[Ruby] Insert, replace, destroy at the end of the character string [b021]
[Rails] How to omit the display of the character string of the link_to method
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
I tried to summarize the methods of Java String and StringBuilder
Is the version of Elasticsearch you are using compatible with Java 11?
Investigation method when the CPU of the server running java is heavy