??合并空字符串?
我发现自己越来越多地做的事情是检查字符串是否为空(如 ""
或 null)和条件运算符。
当前的示例:
s.SiteNumber.IsNullOrEmpty() ? "No Number" : s.SiteNumber;
这只是一个扩展方法,它相当于:
string.IsNullOrEmpty(s.SiteNumber) ? "No Number" : s.SiteNumber;
由于它是空的且不为 null,因此 ??
不会起作用。 ??
的 string.IsNullOrEmpty()
版本将是完美的解决方案。我认为必须有一种更干净的方法来做到这一点(我希望!),但我一直找不到它。
有谁知道更好的方法来做到这一点,即使它只在 .Net 4.0 中?
Something I find myself doing more and more is checking a string for empty (as in ""
or null) and a conditional operator.
A current example:
s.SiteNumber.IsNullOrEmpty() ? "No Number" : s.SiteNumber;
This is just an extension method, it's equivalent to:
string.IsNullOrEmpty(s.SiteNumber) ? "No Number" : s.SiteNumber;
Since it's empty and not null, ??
won't do the trick. A string.IsNullOrEmpty()
version of ??
would be the perfect solution. I'm thinking there has to be a cleaner way of doing this (I hope!), but I've been at a loss to find it.
Does anyone know of a better way to do this, even if it's only in .Net 4.0?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
C# 已经允许我们用
??
替换null
的值。因此,我们需要的只是一个将空字符串转换为null
的扩展,然后我们像这样使用它:扩展类:
C# already lets us substitute values for
null
with??
. So all we need is an extension that converts an empty string tonull
, and then we use it like this:Extension class:
没有内置的方法可以做到这一点。但是,您可以使扩展方法返回字符串或 null,这将允许合并运算符起作用。然而,这会很奇怪,我个人更喜欢你目前的方法。
既然您已经在使用扩展方法,为什么不直接创建一个返回值或默认值的扩展方法:
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:
我知道这是一个老问题 - 但我一直在寻找答案,但以上都不符合我的需要以及我最终使用的内容:
用法:
编辑:
编写此函数的更紧凑的方法是:
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:
Usage:
EDIT:
An even more compact way of writing this function would be:
我有几个我喜欢使用的实用程序扩展:
如果您希望它适用于任何类型,您也可以使用它:
编辑:受到sfsr的答案的启发,我将添加这个从现在开始我的工具箱的变体:
I have a couple of utility extensions that I like to use:
You could also use this if you want it to apply to any type:
Edit: Inspired by sfsr's answer, I'll be adding this variant to my toolbox from now on:
我只是使用 NullIfEmpty 扩展方法,如果字符串为空,则该方法将始终返回 null 允许 ?? (空合并运算符)正常使用。
这然后允许?正常使用并使链接易于阅读。
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.
This then allows ?? to be used as normal and makes chaining easy to read.
空合并运算符的优点之一是它是短路的。当第一部分不为空时,不计算第二部分。当回退需要昂贵的操作时,这可能很有用。
我最终得到:
用法:
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:
Usage:
可能比之前提出的扩展方法稍快一些:
A slightly faster extension method than proposed earlier perhaps:
字符串扩展方法 ValueOrDefault() 怎么样,
或者如果字符串为空则返回 null:
但没有尝试这些解决方案。
how about a string extension method ValueOrDefault()
or return null if string is Empty:
Didn't try these solutions though.
自从写这个答案以来,我已经大大改进了我的“合并”方法,并且还抽象了它,添加了泛型类型的版本。我的解决方案现在由以下三个方法组成:
CoalesceWhiteSpace
将接受任意数量的字符串与“源”字符串合并,将空格视为null
。本文之前版本中使用的相同示例现在如下所示:通用
Coalesce
类似,但接受任何nullable
类型。当然,它也可以与字符串一起使用:但也可以与任何对象一起使用:
最后,
CoalesceDefault
将通过将其与其默认值进行比较来合并任意数量的对象(对于使用对于初始化为默认值的不可空类型,例如DateTime
):它也适用于字符串:
注意:null、"" 和 " " 被视为默认字符串值,因此仅上面返回了最后一个带有多个空格的字符串!
原始答案
我正在使用我自己的字符串合并扩展方法。由于这里使用的是 LINQ,并且绝对浪费资源进行时间密集型操作(我在紧密循环中使用它),因此我将分享我的:
我认为这非常简单,您甚至不需要为 null 烦恼字符串值。像这样使用它:
我经常使用它。第一次使用后您就离不开的“实用”功能之一:-)
Since writing this answer I've improved my "coalesce" method considerably, and also abstracted it adding versions for generic types. My solution now consists of the three methods bellow:
The
CoalesceWhiteSpace
will accept an arbitrary number of strings to coalesce with a "source" string, considering whitespace asnull
. The same example used on the previous version of this post now looks like this:The generic
Coalesce<T>
is similar but accepts anynullable
type. And, of course, it can be used with strings also:But also with any object:
And, finally, the
CoalesceDefault
will coalesce an arbitrary number of objects by comparing it with it's default value (useful to use with non-nullable types which are initialized to their default values, likeDateTime
):It also works with strings:
Note: null, "" and " " are considered default string values, and, as such only the last string with multiple spaces is returned above!
ORIGINAL ANSWER
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:
I think it is quite simple, and you don't even need to bother with null string values. Use it like this:
I use it a lot. One of those "utility" functions you can't live without after first using it :-)
我喜欢下面的扩展方法
QQQ
的简洁性,当然,像这样的运算符?会更好。但是我们可以通过允许比较两个而不是三个字符串选项值来提高这一点,其中一个需要时不时地处理(请参见下面的第二个函数)。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).很容易将一些答案转换为具有泛型的辅助扩展类,以实现更广泛的用途:
注意:有关短路方法的说明,请参阅 wensveen回答
示例用法:
(PS:我想我在这里使用泛型有点偏离主题。我是不是想太多了?)
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
Example usage:
(PS: I think I'm getting bit off topic here using generics. Am I overthinking this?)
不知道,也许我回答这个问题太晚了,但在我的示例中,我将常规三元 ?: 混合到空合并运算符 ??为了让这个也能处理空字符串。
希望它有帮助:)
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 :)