String 변수의 Null 또는 빈문자열 체크하기: StringUtils의 사용

반응형
728x90
반응형

StringUtils

if (StringUtils.isEmpty(test)) {

 

test변수가 빈문자열인지, NULL인지 StringUtils 클래스의 isEmpty 메소드를 통해 검사한다. 해당 isEmpty를 보기위해 StringUtils 클래스 파일을 읽어보았다.

/**
 * Check whether the given object (possibly a {@code String}) is empty.
 * This is effectively a shortcut for {@code !hasLength(String)}.
 * <p>This method accepts any Object as an argument, comparing it to
 * {@code null} and the empty String. As a consequence, this method
 * will never return {@code true} for a non-null non-String object.
 * <p>The Object signature is useful for general attribute handling code
 * that commonly deals with Strings but generally has to iterate over
 * Objects since attributes may e.g. be primitive value objects as well.
 * <p><b>Note: If the object is typed to {@code String} upfront, prefer
 * {@link #hasLength(String)} or {@link #hasText(String)} instead.</b>
 * @param str the candidate object (possibly a {@code String})
 * @since 3.2.1
 * @deprecated as of 5.3, in favor of {@link #hasLength(String)} and
 * {@link #hasText(String)} (or {@link ObjectUtils#isEmpty(Object)})
 */
@Deprecated
public static boolean isEmpty(@Nullable Object str) {
    return (str == null || "".equals(str));
}

 

String 객체가 비어있는지 확인한다. 

return (str == null || "".equals(str)) 문을 통해 빈문자열+NULL 검사를 해주는 것이다.

참고) 
값이 존재하면 ? if(!StringUtils.isEmpty(test)) {}
값이 존재하지 않으면? if(StringUtils.isEmpty(test)) {}

 

 

isEmpty() 메서드 Deprecated

isEmpty() 메서드는 @Deprecated 되었으며, 설명에 아래와 같은 내용이 담겨져있다.

prefer * {@link #hasLength(String)} or {@link #hasText(String)} instead.

 

  • hasText()
/**
 * Check whether the given {@code CharSequence} contains actual <em>text</em>.
 * <p>More specifically, this method returns {@code true} if the
 * {@code CharSequence} is not {@code null}, its length is greater than
 * 0, and it contains at least one non-whitespace character.
 * <p><pre class="code">
 * StringUtils.hasText(null) = false
 * StringUtils.hasText("") = false
 * StringUtils.hasText(" ") = false
 * StringUtils.hasText("12345") = true
 * StringUtils.hasText(" 12345 ") = true
 * </pre>
 * @param str the {@code CharSequence} to check (may be {@code null})
 * @return {@code true} if the {@code CharSequence} is not {@code null},
 * its length is greater than 0, and it does not contain whitespace only
 * @see #hasText(String)
 * @see #hasLength(CharSequence)
 * @see Character#isWhitespace
 */
public static boolean hasText(@Nullable CharSequence str) {
   return (str != null && str.length() > 0 && containsText(str));
}

 

  • hasLength()
/**
 * Check that the given {@code String} is neither {@code null} nor of length 0.
 * <p>Note: this method returns {@code true} for a {@code String} that
 * purely consists of whitespace.
 * @param str the {@code String} to check (may be {@code null})
 * @return {@code true} if the {@code String} is not {@code null} and has length
 * @see #hasLength(CharSequence)
 * @see #hasText(String)
 */
public static boolean hasLength(@Nullable String str) {
   return (str != null && !str.isEmpty());
}

 

반응형

Designed by JB FACTORY