ExtensionMethod.NET Home of 819 C#, Visual Basic, F# and Javascript extension methods

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());

Author: Tom De Wilde

Submitted on: 9 jun 2015

Language: C#

Type: System.String

Views: 2366