Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,987 questions

51,931 answers

573 users

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 149 views
6 answers 591 views
1 answer 156 views
2 answers 177 views
177 views asked Nov 1, 2020 by avibootz
4 answers 313 views
313 views asked Aug 11, 2018 by avibootz
1 answer 154 views
...