[Kotlin] Null vs Empty vs Blank
์ ๊ฐ 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