Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a simpler way to call Contains() on a string that is potentially null?

Tags:

c#

Is there a simpler or less ugly way to call .Contains() on a string that is potentially null then by doing:

Assert.IsTrue((firstRow.Text ?? "").Contains("SomeText"));
like image 426
KingNestor Avatar asked Jan 13 '12 06:01

KingNestor


People also ask

Can string contains null?

An empty string is a String object with an assigned value, but its length is equal to zero. A null string has no value at all. A blank String contains only whitespaces, are is neither empty nor null , since it does have an assigned value, and isn't of 0 length.

Is contain case sensitive?

Contains() method in C# is case sensitive. And there is not StringComparison parameter available similar to Equals() method, which helps to compare case insensitive.

How check string is null or not in C#?

In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned “” or String.


1 Answers

I'd suggest using Assert.That syntax:

Assert.That(firstRow.Text, Does.Contain("SomeText"));

If you need to check that text is not containing a certain string:

Assert.That(firstRow.Text, Does.Not.Contain("SomeText"));
like image 166
Sly Avatar answered Sep 22 '22 13:09

Sly