C# 检查字符串是否为null或空,否则将对其进行修剪

C# 检查字符串是否为null或空,否则将对其进行修剪,c#,null,string,C#,Null,String,我尝试了以下方法: dummy.Title = ds1Question.Title.null ? "Dummy title" : ds1Question.Title.Trim(); 我本来希望在intellisense上看到类似于nullorempty的东西,但似乎没有什么可以做到这一点。有没有其他方法可以做到这一点?这是无效的: ds1Question.Title.null 你可以有: dummy.Title = ds1Question.Title == null ? "Dummy ti

我尝试了以下方法:

dummy.Title = ds1Question.Title.null ? "Dummy title" : ds1Question.Title.Trim();
我本来希望在intellisense上看到类似于
nullorempty
的东西,但似乎没有什么可以做到这一点。有没有其他方法可以做到这一点?

这是无效的:

 ds1Question.Title.null
你可以有:

dummy.Title = ds1Question.Title == null ? "Dummy title"
                                        : ds1Question.Title.Trim();
或使用:

dummy.Title = (ds1Question.Title ?? "Dummy title").Trim();
这将对默认值执行不必要的修剪,但很简单

但是,这些将只检查空值。为了检查是否为空,您需要调用,我会通过一个额外的变量来检查:

string title = ds1Question.Title;
dummy.Title = string.IsNullOrEmpty(title) ? "Dummy title" : title.Trim();
或者按照Marc的答案使用,以避免标题为“”,在修剪之前它不是空的。

也许:

dummy.Title = string.IsNullOrEmpty(ds1Question.Title)
             ? "Dummy title" : ds1Question.Title.Trim();


您必须通过字符串静态方法调用它

dummy.Title = string.IsNullOrEmpty(ds1Question.Title) ? "Dummy title" : ds1Question.Title.Trim();
如果您希望能够直接在string实例上调用它,当然可以添加如下extensionmethod

public static bool IsNullOrEmpty(this string str)
{
    return string.IsNullOrEmpty(str);
}
那你可以用

ds1Question.Title.IsNullOrEmpty() ? "Dummy title" : ds1Question.Title.Trim();

你几乎成功了。试试这个:

dummy.Title = 
    string.IsNullOrEmpty(ds1Question.Title) ? 
    "Dummy title" : 
    ds1Question.Title.Trim();
dummy.Title=string.IsNullOrEmpty(ds1Question.Title)?“虚拟标题” :ds1Question.Title.Trim()


您可以进一步了解Justin Harvey的内容,并实现一个扩展方法(当然是在静态类中),如下所示:

public static string TrimmedOrDefault(this string str, string def)
{
    if (string.IsNullOrEmpty(str)) //or if (string.IsNullOrWhiteSpace(str))
    {
        // Hmm... what if def is null or empty?
        // Well, I guess that's what the caller wants.
        return def; 
    }
    else
    {
        return str.Trim();
    }
}
dummy.Title = ds1Question.Title.TrimmedOrDefault("Dummy title");
然后你可以这样使用它:

public static string TrimmedOrDefault(this string str, string def)
{
    if (string.IsNullOrEmpty(str)) //or if (string.IsNullOrWhiteSpace(str))
    {
        // Hmm... what if def is null or empty?
        // Well, I guess that's what the caller wants.
        return def; 
    }
    else
    {
        return str.Trim();
    }
}
dummy.Title = ds1Question.Title.TrimmedOrDefault("Dummy title");

下面是我使用的一些字符串扩展名。我加了一个做安全修剪。希望有人会发现这些有用

    /// <summary>
    /// Extensions for String
    /// </summary>
    public static class StringExtenions
    {
        public static string SafeTrim(this string input)
        {
            if (input.IsNotEmpty())
            {
                return input.Trim();
            }

            return input;
        }

        /// <summary>
        /// Checks to see if a given string is empty.
        /// </summary>        
        public static bool IsEmpty(this string input)
        {
            return string.IsNullOrEmpty(input);
        }

        /// <summary>
        /// Checks to see if a given string is not empty.
        /// </summary>        
        public static bool IsNotEmpty(this string input)
        {
            return !string.IsNullOrEmpty(input);
        }

        /// <summary>
        /// Converts text to title case.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToTitleCase(this string input)
        {
            CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;            
            TextInfo textInfo = cultureInfo.TextInfo;

            return textInfo.ToTitleCase(input.ToLower());
        }
    }
//
///字符串的扩展名
/// 
公共静态类字符串扩展
{
公共静态字符串SafeTrim(此字符串输入)
{
if(input.IsNotEmpty())
{
返回input.Trim();
}
返回输入;
}
/// 
///检查给定字符串是否为空。
///         
公共静态bool为空(此字符串输入)
{
返回字符串.IsNullOrEmpty(输入);
}
/// 
///检查给定字符串是否为空。
///         
公共静态bool IsNotEmpty(此字符串输入)
{
return!string.IsNullOrEmpty(输入);
}
/// 
///将文本转换为标题大小写。
/// 
/// 
/// 
公共静态字符串ToTitleCase(此字符串输入)
{
CultureInfo CultureInfo=Thread.CurrentThread.CurrentCulture;
TextInfo TextInfo=cultureInfo.TextInfo;
返回textInfo.ToTitleCase(input.ToLower());
}
}

在C#6.0中,我们现在可以使用null条件运算符,如ds1Question.Title?.Trim()它不允许我只编辑一个字符,但如果有人仍然无意中发现了这一点,替代方法应该是“IsNullOrWhiteSpace”而不是“IsNullOrWhiteSpace”