String.IsNotNullThenTrim
Perform a Trim() when the string is not null. If the string is null the method will return null.
Source
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());