??是否合并空字符串?

我发现自己做得越来越多的事情是检查字符串是否为空(如""或NULL)和条件运算符。

当前示例:

s.SiteNumber.IsNullOrEmpty() ? "No Number" : s.SiteNumber;

这只是一个扩展方法,它相当于:

string.IsNullOrEmpty(s.SiteNumber) ? "No Number" : s.SiteNumber;

因为它是空的,而不是NULL,所以??不起作用。??string.IsNullOrEmpty()版本将是完美的解决方案。我在想一定有一种更干净的方法来做这件事(我希望!),但我一直在寻找它。

有没有人知道一个更好的方法来做到这一点,即使它只是在.NET4.0?

88595 次浏览

There isn't a built-in way to do this. You could make your extension method return a string or null, however, which would allow the coalescing operator to work. This would be odd, however, and I personally prefer your current approach.

Since you're already using an extension method, why not just make one that returns the value or a default:

string result = s.SiteNumber.ConvertNullOrEmptyTo("No Number");

C# already lets us substitute values for null with ??. So all we need is an extension that converts an empty string to null, and then we use it like this:

s.SiteNumber.NullIfEmpty() ?? "No Number";

Extension class:

public static class StringExtensions
{
public static string NullIfEmpty(this string s)
{
return string.IsNullOrEmpty(s) ? null : s;
}
public static string NullIfWhiteSpace(this string s)
{
return string.IsNullOrWhiteSpace(s) ? null : s;
}
}

how about a string extension method ValueOrDefault()

public static string ValueOrDefault(this string s, string sDefault)
{
if (string.IsNullOrEmpty(s))
return sDefault;
return s;
}

or return null if string is Empty:

public static string Value(this string s)
{
if (string.IsNullOrEmpty(s))
return null;
return s;
}

Didn't try these solutions though.

I know this is an old question - but I was looking for an answer and none of the above fit my need as well as what I ended up using:

private static string Coalesce(params string[] strings)
{
return strings.FirstOrDefault(s => !string.IsNullOrEmpty(s));
}

Usage:

string result = Coalesce(s.SiteNumber, s.AltSiteNumber, "No Number");

EDIT: An even more compact way of writing this function would be:

static string Coalesce(params string[] strings) => strings.FirstOrDefault(s => !string.IsNullOrEmpty(s));

I have a couple of utility extensions that I like to use:

public static string OrDefault(this string str, string @default = default(string))
{
return string.IsNullOrEmpty(str) ? @default : str;
}


public static object OrDefault(this string str, object @default)
{
return string.IsNullOrEmpty(str) ? @default : str;
}

You could also use this if you want it to apply to any type:

public static T OrDefault<T>(this T obj, T @default)
{
return EqualityComparer<T>.Default.Equals(obj, default(T)) ? @default : obj;
}

Edit: Inspired by sfsr's answer, I'll be adding this variant to my toolbox from now on:

public static string Coalesce(this string str, params string[] strings)
{
return (new[] {str})
.Concat(strings)
.FirstOrDefault(s => !string.IsNullOrEmpty(s));
}

A slightly faster extension method than proposed earlier perhaps:

public static string Fallback(this string @this, string @default = "")
{
return (@this == null || @this.Trim().Length == 0) ? @default : @this;
}

I'm using a string Coalesce extension method of my own. Since those here are using LINQ, and absolutelly wasting resources for time intensive operations (I'm using it in tight loops), I'll share mine:

public static class StringCoalesceExtension
{
public static string Coalesce(this string s1, string s2)
{
return string.IsNullOrWhiteSpace(s1) ? s2 : s1;
}
}

I think it is quite simple, and you don't even need to bother with null string values. Use it like this:

string s1 = null;
string s2 = "";
string s3 = "loudenvier";
string s = s1.Coalesce(s2.Coalesce(s3));
Assert.AreEqual("loudenvier", s);

I use it a lot. One of those "utility" functions you can't live without after first using it :-)

I like the brevity of the following extension method QQQ for this, though of course an operator like? would be better. But we can 1 up this by allowing not just two but three string option values to be compared, which one encounters the need to handle every now and then (see second function below).

#region QQ


[DebuggerStepThrough]
public static string QQQ(this string str, string value2)
{
return (str != null && str.Length > 0)
? str
: value2;
}


[DebuggerStepThrough]
public static string QQQ(this string str, string value2, string value3)
{
return (str != null && str.Length > 0)
? str
: (value2 != null && value2.Length > 0)
? value2
: value3;
}




// Following is only two QQ, just checks null, but allows more than 1 string unlike ?? can do:


[DebuggerStepThrough]
public static string QQ(this string str, string value2, string value3)
{
return (str != null)
? str
: (value2 != null)
? value2
: value3;
}


#endregion

One of the advantages of the null-coalescing operator is that it short-circuits. When the first part isn't null, the second part isn't evaluated. This can be useful when the fallback requires an expensive operation.

I ended up with:

public static string Coalesce(this string s, Func<string> func)
{
return String.IsNullOrEmpty(s) ? func() : s;
}

Usage:

string navigationTitle = model?.NavigationTitle.
Coalesce(() => RemoteTitleLookup(model?.ID)). // Expensive!
Coalesce(() => model?.DisplayName);

I simply use a NullIfEmpty extension method which will always return null if the string is empty allowing ?? (Null Coalescing Operator) to be used as normal.

public static string NullIfEmpty(this string s)
{
return string.IsNullOrEmpty(s) ? null : s;
}

This then allows ?? to be used as normal and makes chaining easy to read.

string string1 = string2.NullIfEmpty() ?? string3.NullIfEmpty() ?? string4;

It's easy to turn some of the answers in to a helper extension class with generics for a even broader usage:

NOTE: for an explanation of short-circuit method the see the wensveen answer

// classic
public static string Coalesce(this string s, params string[] strings)
=> s.Coalesce(string.IsNullOrEmpty, strings);


// short-circuit compatible, for expensive string getting
public static string Coalesce(this string s, params Func<string>[] getters)
=> s.Coalesce(string.IsNullOrEmpty, getters);


// generic
public static T Coalesce<T>(this T value, Func<T, bool> isEmpty, params T[] values) where T : class
=> isEmpty(value) ? values.FirstOrDefault(val => !isEmpty(val)) : value;


// generic, short-circuit compatible
public static T Coalesce<T>(this T value, Func<T, bool> isEmpty, params Func<T>[] getters) where T : class {
if (isEmpty(value))
return getters
.Select(getter => new Lazy<T>(getter))
.FirstOrDefault(val => !isEmpty(val.Value))
?.Value;


return value;
}

Example usage:

string result = s.SiteNumber.Coalesce(s.AltSiteNumber, "No Number");


string result = s.SiteNumber.Coalesce(string.IsNullOrWhiteSpace, s.AltSiteNumber, "No Number");


string navigationTitle = model?.NavigationTitle.
Coalesce(() => RemoteTitleLookup(model?.ID), () => model?.DisplayName);


Player player = player1.Coalesce(p => p?.Score > 0, player2, player3);

(PS: I think I'm getting bit off topic here using generics. Am I overthinking this?)

sqlCom.Parameters.Add(new SqlParameter("@qavCode", SqlDbType.Char, 11)).Value = (object)(string.IsNullOrEmpty(rf.Request.QavCode) ? null : rf.Request.QavCode) ?? DBNull.Value;

Don't know, maybe I'm too late answering this question but in my example I mixed the regular ternary ?: to the null-coalescing operator ?? in order this one to work with empty strings too. Hope it helps :)