فرق بین String.IsNullOrEmpty و String.IsNullOrWhiteSpace در چیست؟
200, OK
https://www.code4it.dev:443/csharptips/string-isnullorempty-isnullorwhitespace/ icon

C# tip: String.IsNullOrEmpty or String.IsNullOrWhiteSpace?  

//So we have:

String.IsNullOrEmpty(""); //True
String.IsNullOrEmpty(null); //True
String.IsNullOrEmpty("   "); //False
String.IsNullOrEmpty("\n"); //False
String.IsNullOrEmpty("\t"); //False
String.IsNullOrEmpty("hello"); //False

//but also

String.IsNullOrWhiteSpace("");//True
String.IsNullOrWhiteSpace(null);//True
String.IsNullOrWhiteSpace("   ");//True
String.IsNullOrWhiteSpace("\n");//True
String.IsNullOrWhiteSpace("\t");//True
String.IsNullOrWhiteSpace("hello");//False
فرق بین String.IsNullOrEmpty و String.IsNullOrWhiteSpace در چیست؟
  • #
    ‫۳ سال قبل، یکشنبه ۱۷ مرداد ۱۴۰۰، ساعت ۰۷:۲۰
    string.IsNullOrEmpty(sampleString?.Trim())  = string.IsNullOrWhiteSpace(sampleString) 
    دو تابع فوق نتایج یکسانی را بدست می‌دهند. هرچند استفاده از IsNullOrWhiteSpace از نظر سرعت بهینه‌تر است