Check string is null or empty in linq

后端 未结 3 926
一生所求
一生所求 2021-01-21 23:24

I have string with empty space(\"________\")

string MyNote= Convert.ToString(Session[\"MyNote\"]);

if(MyNote!=null || MyNote != \"\")
{


}


        
相关标签:
3条回答
  • 2021-01-21 23:51
    if(MyNote!=null || MyNote.Length > 0) //or you may want to set different value than 0
    {
    
    
    }
    
    0 讨论(0)
  • 2021-01-21 23:59

    String.IsNullOrWhiteSpace is the method you're looking for.

    Indicates whether a specified string is null, empty, or consists only of white-space characters.

    Alternatively, using your idea:

    if(MyNote!=null && MyNote.Trim() != "")
    {
    
    }
    

    or

    if(MyNote!=null && MyNote.Trim().Length == 0)
    {
    
    }
    
    0 讨论(0)
  • 2021-01-22 00:02

    This works for me:

     string MyNote = Session["MyNote"] == null ? String.Empty : Session["MyNote"].ToString();
    
    0 讨论(0)
提交回复
热议问题