관리 메뉴

웹개발자의 기지개

[C#] string.IsNullOrEmpty 와 string.IsNullOrWhiteSpace 본문

ASP.NET/C#

[C#] string.IsNullOrEmpty 와 string.IsNullOrWhiteSpace

http://portfolio.wonpaper.net 2020. 10. 10. 00:30

문자열을 null 이거나 빈문자열등을 확인할때 쓰는 두가지 메소드인데

약간의 차이점이 존재한다.

 

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

 

참고 : egloos.zum.com/Ibki/v/521442

Comments