String.IsNotNullThenTrim

public static string IsNotNullThenTrim(this string s)
{
    if (!string.IsNullOrEmpty(s))
        return s.Trim();
    else
        return s;
}
Example:
string n = null;
Assert.IsNull(n.IsNotNullThenTrim());

string s = "test ";
Assert.IsNotNull(s.IsNotNullThenTrim());
Assert.AreEqual("test", s.IsNotNullThenTrim());

Description

Perform a Trim() when the string is not null. If the string is null the method will return null.

Details

Double click on the code to select all.

 

;