C# If string is not null or empty else 一个衬里

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15660461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:31:09  来源:igfitidea点击:

One liner for If string is not null or empty else

c#if-statementisnullorempty

提问by user2140261

I usually use something like this for various reasons throughout an application:

我通常在整个应用程序中出于各种原因使用这样的东西:

if (String.IsNullOrEmpty(strFoo))
{
     FooTextBox.Text = "0";
}
else
{
     FooTextBox.Text = strFoo;
}

If I'm going to be using it a lot I will create a method that returns the desired string. For example:

如果我要经常使用它,我将创建一个返回所需字符串的方法。例如:

public string NonBlankValueOf(string strTestString)
{
    if (String.IsNullOrEmpty(strTestString))
        return "0";
    else
        return strTestString;
}

and use it like:

并使用它:

FooTextBox.Text = NonBlankValueOf(strFoo);

I always wondered if there was something that was part of C# that would do this for me. Something that could be called like:

我一直想知道是否有 C# 的一部分可以为我做这件事。可以这样称呼的东西:

FooTextBox.Text = String.IsNullOrEmpty(strFoo,"0")

the second parameter being the returned value if String.IsNullOrEmpty(strFoo) == true

第二个参数是返回值,如果 String.IsNullOrEmpty(strFoo) == true

If not does anyone have any better approaches they use?

如果没有,有没有人有更好的方法?

采纳答案by Anthony Pegram

There is a null coalescing operator (??), but it would not handle empty strings.

有一个空合并运算符 ( ??),但它不会处理空字符串。

If you were only interested in dealing with null strings, you would use it like

如果你只对处理空字符串感兴趣,你会像这样使用它

string output = somePossiblyNullString ?? "0";

For your need specifically, there is simply the conditional operator bool expr ? true_value : false_valuethat you can use to simply if/else statement blocks that set or return a value.

根据您的具体需要bool expr ? true_value : false_value,您可以使用简单的条件运算符来简单地设置或返回值的 if/else 语句块。

string output = string.IsNullOrEmpty(someString) ? "0" : someString;

回答by Hossein Narimani Rad

This may help:

这可能有帮助:

public string NonBlankValueOf(string strTestString)
{
    return String.IsNullOrEmpty(strTestString)? "0": strTestString;
}

回答by Jim Mischel

You could use the ternary operator:

您可以使用三元运算符

return string.IsNullOrEmpty(strTestString) ? "0" : strTestString

FooTextBox.Text = string.IsNullOrEmpty(strFoo) ? "0" : strFoo;

回答by ssilas777

You can write your own Extensionmethod for type String :-

您可以为 String 类型编写自己的扩展方法:-

 public static string NonBlankValueOf(this string source)
 {
    return (string.IsNullOrEmpty(source)) ? "0" : source;
 }

Now you can use it like with any string type

现在您可以像使用任何字符串类型一样使用它

FooTextBox.Text = strFoo.NonBlankValueOf();

回答by dathompson

Old question, but thought I'd add this to help out,

老问题,但我想我会添加这个来帮助,

#if DOTNET35
bool isTrulyEmpty = String.IsNullOrEmpty(s) || s.Trim().Length == 0;
#else
bool isTrulyEmpty = String.IsNullOrWhiteSpace(s) ;
#endif