Android/Android Studio

[Kotlin] Null vs Empty vs Blank

MK_____ 2022. 5. 12. 17:22

 

์ œ๊ฐ€ empty์™€ blank๊ฐ€ ๋งค์ผ ํ—ท๊ฐˆ๋ ค์„œ ํ™•์‹คํ•˜๊ฒŒ ์งš๊ณ  ๋„˜์–ด๊ฐ€๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
String Validation ํ™•์ธ ์ค‘, ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” Null, Empty, Blank์˜ ์ฐจ์ด๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
์•„๋ž˜์˜ ์ฝ”๋“œ๋“ค์€ group: org.apache.commons, name: commons-lang3, version: 3.11 ์ž…๋‹ˆ๋‹ค.


Null

๋ณ€์ˆ˜์— ์•„๋ฌด๊ฒƒ๋„ ํ• ๋‹น๋˜์ง€(์ฐธ์กฐํ•˜์ง€) ์•Š์€ ์ƒํƒœ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.


Empty

empty(๋น„์–ด์žˆ๋Š”) : ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ 0์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

์•„๋ž˜๋Š” StringUtils.isEmpty ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
๋ˆˆ์—ฌ๊ฒจ ๋ณด์‹ค ์ ์€ ""๊ณผ " "์˜ ์ฐจ์ด์ž…๋‹ˆ๋‹ค. empty๋Š” length = 0์„ ์˜๋ฏธํ•˜๊ธฐ ๋•Œ๋ฌธ์— " "๊ฐ’์€ false๋กœ ๋ฐ˜ํ™˜ ๋ฉ๋‹ˆ๋‹ค.

StringUtils.isEmpty

// Empty checks
//-----------------------------------------------------------------------
/**
 * <p>Checks if a CharSequence is empty ("") or null.</p>
 *
 * <pre>
 * StringUtils.isEmpty(null)      = true
 * StringUtils.isEmpty("")        = true
 * StringUtils.isEmpty(" ")       = false
 * StringUtils.isEmpty("bob")     = false
 * StringUtils.isEmpty("  bob  ") = false
 * </pre>
 *
 * <p>NOTE: This method changed in Lang version 2.0.
 * It no longer trims the CharSequence.
 * That functionality is available in isBlank().</p>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if the CharSequence is empty or null
 * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
 */
public static boolean isEmpty(final CharSequence cs) {
   return cs == null || cs.length() == 0;
}

Blank

blank(๊ณต๋ฐฑ) : whitespace(๊ณต๋ฐฑ, ๋„์–ด์“ฐ๊ธฐ)๋งŒ ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

StringUtils.isBlank

/**
 * <p>Checks if a CharSequence is empty (""), null or whitespace only.</p>
 *
 * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
 *
 * <pre>
 * StringUtils.isBlank(null)      = true
 * StringUtils.isBlank("")        = true
 * StringUtils.isBlank(" ")       = true
 * StringUtils.isBlank("bob")     = false
 * StringUtils.isBlank("  bob  ") = false
 * </pre>
 *
 * @param cs  the CharSequence to check, may be null
 * @return {@code true} if the CharSequence is null, empty or whitespace only
 * @since 2.0
 * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
 */
public static boolean isBlank(final CharSequence cs) {
   final int strLen = length(cs);
   if (strLen == 0) {
      return true;
   }
   for (int i = 0; i < strLen; i++) {
      if (!Character.isWhitespace(cs.charAt(i))) {
         return false;
      }
   }
   return true;
}

Run code

์•„๋ž˜ 5๊ฐ€์ง€์˜ String Value์— ๋Œ€ํ•œ Null, Empty, Blank ๊ฒฐ๊ณผ ๊ฐ’์ž…๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ ๊ฐ’

String ValueNullEmptyBlank

null true true true
"" false true true
" " false false true
"value" false false false
" v a l u e " false false false

์‹คํ–‰ ์ฝ”๋“œ

log.info("{}", null == null);
log.info("{}", "" == null);
log.info("{}", " " == null);
log.info("{}", "value" == null);
log.info("{}", " v a l u e " == null);

log.info("{}", StringUtils.isEmpty(null));
log.info("{}", StringUtils.isEmpty(""));
log.info("{}", StringUtils.isEmpty("  "));
log.info("{}", StringUtils.isEmpty("value"));
log.info("{}", StringUtils.isEmpty(" v a l u e "));

log.info("{}", StringUtils.isBlank(null));
log.info("{}", StringUtils.isBlank(""));
log.info("{}", StringUtils.isBlank("  "));
log.info("{}", StringUtils.isBlank("value"));
log.info("{}", StringUtils.isBlank(" v a l u e "));

 

 

์ถœ์ฒ˜: https://velog.io/@sixhustle/null-empty-blank

 

Null vs Empty vs Blank

-

velog.io