Testing fro null or empty string

I know this is a basic C# question but I cannot remember the answer.
If I want to test for the string condition before processing can I use
var strTest = null;
if(strTest == null || strTest.trim() == string.Empty)
....

Of do I have to break them up like
if(strTest == null)
   return;
if(strTest.trim() != string.Empty)
   Do something here......

Thank you.
lapuccaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

p_davisCommented:
if(String.IsNullOrEmpty(yourString))

{}
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lapuccaAuthor Commented:
Perfect!  Thank you.
0
it_saigeDeveloperCommented:
As p_davis said:
using System;

namespace EE_Q28702578
{
	class Program
	{
		static string s1 = default(string);	// Null string
		static string s2 = "";				// Empty string
		static string s3 = " ";				// Whitespace string - technically not empty
		static string s4 = "Unempty string";	// Self explanatory

		static void Main(string[] args)
		{
			if (s1.IsNullOrWhiteSpace())
				Console.WriteLine("s1 is a null or whitespace string.");
			else
				Console.WriteLine("s1 is not a null or whitespace string.");

			if (s2.IsNullOrWhiteSpace())
				Console.WriteLine("s2 is a null or whitespace string.");
			else
				Console.WriteLine("s2 is not a null or whitespace string.");

			if (s3.IsNullOrWhiteSpace())
				Console.WriteLine("s3 is a null or whitespace string.");
			else
				Console.WriteLine("s3 is not a null or whitespace string.");

			if (s4.IsNullOrWhiteSpace())
				Console.WriteLine("s4 is a null or whitespace string.");
			else
				Console.WriteLine("s4 is not a null or whitespace string.");

			Console.ReadLine();
		}
	}

	static class Extensions
	{
		// We have to implement this prior to .NET 4...
		public static bool IsNullOrWhiteSpace(this string source)
		{
			return string.IsNullOrEmpty(source) || source.Trim().Length == 0;
		}
	}
}

Open in new window

Which produces the following output -Capture.JPG***NOTE***
IsNullOrWhiteSpace is native in .NET 4 and above, it has to be implemented in .NET 3.5 and below.

-saige-
1
lapuccaAuthor Commented:
Thank you Saige,
I forgot to mention I'm using .net 4.5 so it works for me.  Thank you for the reply.  I'm sorry that I already give points away but I will mark yours as good comment.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.