본문 바로가기

개발/C#

string.IsNullOrEmpty 와 string.IsNullOrWhiteSpace 비교

string.IsNullOrEmpty 와 string.IsNullOrWhiteSpace 비교


1. string.IsNullOrEmpty 
 - 지정된 문자열이 null이거나 Empty 문자열인지 여부를 나타냄
 ※ string.Empty는 길이가 0인 문자열 ""을 나타냄
bool result;
result = string.IsNullOrEmpty(null);        // true
result = string.IsNullOrEmpty("");          // true
result = string.IsNullOrEmpty(" ");         // false
result = string.IsNullOrEmpty("Test");      // false

2. string.IsNullOrWhiteSpace
 - 지정된 문자열이 null이거나 비어 있거나 공백 문자로만 구성되어 있는지 여부를 나타냄
 ※ WhiteSpace는 다음과 같음
  1) " "
  2) ""
  3) "\r\n\v\t"와 같은 문자

bool result;

result = string.IsNullOrWhiteSpace(null);   // true

result = string.IsNullOrWhiteSpace("");     // true

result = string.IsNullOrWhiteSpace(" ");    // true

result = string.IsNullOrWhiteSpace("\t");   // true

result = string.IsNullOrWhiteSpace("Test"); // false


※ 출처

1) http://blog.daum.net/_blog/BlogTypeView.do?blogid=0K2Xg&articleno=433&categoryId=372549&regdt=20131205121812

2) http://six605.tistory.com/447