How to check if string is empty in C#

4 Answers

0 votes
using System;
 
public class Program
{
    static void Main(string[] args)
    {
        try {
            string s = null;
 
            if (s == string.Empty) {
                Console.WriteLine("yes");
            }
            else {
                Console.WriteLine("no"); 
            }
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }  
    }
}



/*
run:

no
  
*/


answered Mar 22, 2015 by avibootz
edited May 14, 2024 by avibootz
0 votes
using System;
 
public class Program
{
    static void Main(string[] args)
    {
        try {
            string s = "";
 
            if (s == string.Empty) {
                Console.WriteLine("yes");
            }
            else {
                Console.WriteLine("no"); 
            }
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }  
    }
}



/*
run:

yes
  
*/


answered Mar 22, 2015 by avibootz
edited May 14, 2024 by avibootz
0 votes
using System;
 
public class Program
{
    static void Main(string[] args)
    {
        try {
            string s = string.Empty;
 
            if (s == string.Empty) {
                Console.WriteLine("yes");
            }
            else {
                Console.WriteLine("no"); 
            }
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }  
    }
}



/*
run:

yes

*/
  


answered Mar 22, 2015 by avibootz
edited May 14, 2024 by avibootz
0 votes
using System;
 
public class Program
{
    static void Main(string[] args)
    {
        string s = "";
 
        if (s == "") {
            Console.WriteLine("empty 1");
        }
 
        if (s.Equals("")) {
            Console.WriteLine("empty 2");
        }
 
        if (string.Equals(s, "")) {
            Console.WriteLine("empty 3");
        }
 
        if (s.Length == 0) {
            Console.WriteLine("empty 4");
        }
 
        if (string.IsNullOrEmpty(s)) {
            Console.WriteLine("empty 5");
        }
    }
}



/*
run:

empty 1
empty 2
empty 3
empty 4
empty 5
  
*/

 



answered May 14, 2024 by avibootz

Related questions

1 answer 260 views
6 answers 939 views
1 answer 295 views
2 answers 351 views
351 views asked Nov 1, 2020 by avibootz
4 answers 524 views
524 views asked Aug 11, 2018 by avibootz
1 answer 284 views
...