C#의 IsNullOrWhiteSpace() 메서드는 지정된 문자열이 null인지, 비어 있는지 또는 공백 문자로만 구성되어 있는지 여부를 나타내는 데 사용됩니다.
구문
public static bool IsNullOrWhiteSpace (string val);
위에서 매개변수 val은 테스트할 문자열입니다. 이제 예제를 살펴보겠습니다. -
예시
이제 예를 살펴보겠습니다.
using System;
public class Demo {
public static void Main() {
string str1 = null;
string str2 = String.Empty;
Console.WriteLine("Is string1 null or whitespace? = "+String.IsNullOrWhiteSpace(str1));
Console.WriteLine("Is string2 null or whitespace? = "+String.IsNullOrWhiteSpace(str2));
}
} 출력
그러면 다음과 같은 출력이 생성됩니다. -
Is string1 null or whitespace? = True Is string2 null or whitespace? = True
예시
이제 다른 예를 살펴보겠습니다. -
using System;
public class Demo {
public static void Main() {
string str1 = "\n";
string str2 = "Tim";
Console.WriteLine("Is string1 null or whitespace? = "+String.IsNullOrWhiteSpace(str1));
Console.WriteLine("Is string2 null or whitespace? = "+String.IsNullOrWhiteSpace(str2));
}
} 출력
Is string1 null or whitespace? = True Is string2 null or whitespace? = False