Search Results for

    Show / Hide Table of Contents

    Substring Constraint

    SubstringConstraint tests for a substring.

    Constructor

    SubstringConstraint(string expected)
    

    Syntax

    Does.Contain(string expected)
    

    Modifiers

    ...IgnoreCase
    ...Using(StringComparison comparisonType)
    ...Using(CultureInfo culture)
    

    Examples of Use

    [Test]
    public void StringConstraint_Examples()
    {
        Assert.That("Hello World!", Does.StartWith("Hello"));
        Assert.That("Hello World!", Does.EndWith("World!"));
        Assert.That("Hello World!", Does.Contain("lo Wor"));
        Assert.That("Hello World!", Does.Match("H.*!"));
    }
    

    Specifying a StringComparison

    Assert.That("Hello World!", Does.Contain("WORLD").Using(StringComparison.OrdinalIgnoreCase));
    Assert.That("Hello World!", Does.Contain("World").Using(StringComparison.Ordinal));
    

    Specifying a CultureInfo

    The Using(CultureInfo) modifier allows for culture-specific string comparisons. It can be combined with .IgnoreCase for case-insensitive culture-aware comparisons:

    // Using Turkish culture where 'i' and 'I' have special casing rules
    Assert.That("Hello TITLE World", Does.Contain("title").IgnoreCase.Using(new CultureInfo("tr-TR")));
    
    // Culture-specific comparison without case-insensitivity
    Assert.That("Straße Street", Does.Contain("Straße").Using(new CultureInfo("de-DE")));
    

    Notes

    1. Only one Using modifier may be specified. Attempting to use multiple Using modifiers will throw an InvalidOperationException.
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0