여러분은 이전에 asp.net 2.x 버전부터 제공한 IsNullOrEmpty() 메소드를 아실 겁니다.
간략이 말을 해서 IsNullOrWhiteSpace() ... 이 메소드는 빈 공간까지 잡아내는 메소드 입니다.
static void Main(string[] args)
{
string helloString = "Hello, world!";
string nullString = null;
string emptyString = string.Empty;
string whiteSpaceString = "\t\r\n ";
Console.WriteLine("Is null or whitespace?");
Console.WriteLine("-----------------------");
Console.WriteLine("helloString: " + string.IsNullOrWhiteSpace(helloString));
Console.WriteLine("nullString: " + string.IsNullOrWhiteSpace(nullString));
Console.WriteLine("emptyString: " + string.IsNullOrWhiteSpace(emptyString));
Console.WriteLine("whiteSpaceString: " +string.IsNullOrWhiteSpace(whiteSpaceString));
Console.ReadLine();
}
자.... 위의 결과 출력은 어떻게 나올까요??
helloString: False
nullString: True
emptyString: True
whiteSpaceString: True
이는 bool 형이며, true 및 false 를 반환합니다.
위의 함수를 굳이 asp.net 2.x 버전에서도 사용하고 싶다면 간단히 extension method 나 static 메소드를 간단히 만들어서 사용하면 되겠습니다.
public static bool IsNullOrWhiteSpace(string s)
{
if (s == null)
return true;
return (s.Trim() == string.Empty);
}
감사합니다. posted by 심재운 (shimpark@gmail.com)