문자열을 빈 목록으로 초기화하려면 -
string myStr = null;
이제 내장 메소드 IsNullOrEmpty()를 사용하여 목록이 비어 있는지 여부를 확인하십시오 -
if (string.IsNullOrEmpty(myStr)) {
Console.WriteLine("String is empty or null!");
} 전체 코드를 보자 -
예
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
string myStr = null;
if (string.IsNullOrEmpty(myStr)) {
Console.WriteLine("String is empty or null!");
}
Console.ReadKey();
}
}
} 출력
String is empty or null!
문자열을 빈 문자열로 초기화하는 또 다른 방법은 다음 코드를 시도하십시오. 여기서는 string.Empty −
를 사용했습니다.예
using System;
namespace Demo {
public class Program {
public static void Main(string[] args) {
string myStr = string.Empty;
if (string.IsNullOrEmpty(myStr)) {
Console.WriteLine("String is empty or null!");
} else {
Console.WriteLine("String isn't empty or null!");
}
}
}
} 출력
String is empty or null!