Skip to content

Strings

StringAssertions provides assertions for string values. Obtain one via .Should() on any string.

String comparison methods default to StringComparison.Ordinal. Pass an alternative StringComparison to any method that accepts one.

Null and Empty

string? value = null;

// Requires null
value.Should().BeNull();

// Requires null or empty (does not chain)
value.Should().BeNullOrEmpty();

// Requires null, empty, or whitespace (does not chain)
value.Should().BeNullOrWhiteSpace();

// Requires non-null and non-empty
"hello".Should().NotBeNullOrEmpty();

// Requires non-null, non-empty, non-whitespace
"hello".Should().NotBeNullOrWhiteSpace();

// Requires non-null and length == 0
"".Should().BeEmpty();

// Requires non-null and length > 0
"hello".Should().NotBeEmpty();

Length

"hello".Should().HaveLength(5);
"hello".Should().NotHaveLength(3);

Substring and Prefix/Suffix

"Hello, World!".Should().Contain("World");
"Hello, World!".Should().NotContain("Goodbye");

"Hello, World!".Should().StartWith("Hello");
"Hello, World!".Should().NotStartWith("Goodbye");

"Hello, World!".Should().EndWith("World!");
"Hello, World!".Should().NotEndWith("Hello");

Case-insensitive comparison:

"Hello, World!".Should().Contain("world", StringComparison.OrdinalIgnoreCase);
"Hello, World!".Should().StartWith("hello", StringComparison.OrdinalIgnoreCase);

Regular Expressions

"abc123".Should().Match(@"\d+");
"abcdef".Should().NotMatch(@"\d+");

Chaining

Chainable methods return a StringAssertionsChain:

"Hello, World!"
    .Should()
    .NotBeNullOrEmpty()
    .And.StartWith("Hello")
    .And.EndWith("World!")
    .And.HaveLength(13);