C# .Trim()当字符串为空或null时
我从客户端接收到一些json格式的数据。 我在写这个:C# .Trim()当字符串为空或null时,c#,C#,我从客户端接收到一些json格式的数据。 我在写这个: string TheText; // or whould it be better string TheText = ""; ? TheText = ((serializer.ConvertToType<string>(dictionary["TheText"])).Trim()); string文本;//还是最好将文本“”串起来? TheText=((serializer.ConvertToType(dictionary[“
string TheText; // or whould it be better string TheText = ""; ?
TheText = ((serializer.ConvertToType<string>(dictionary["TheText"])).Trim());
string文本;//还是最好将文本“”串起来?
TheText=((serializer.ConvertToType(dictionary[“TheText”])).Trim());
如果从json解析的变量返回为空,那么当我调用.Trim()方法时,这段代码会崩溃吗
谢谢。- 不,最好将
文本初始化为
”。之后你就分配给它了“
- 不,它不会崩溃–
在空字符串上工作正常。如果“空”的意思是它可以为空,那么是的,它将崩溃;可以使用null条件调用使null保持为null:Trim()
string TheText = serializer.ConvertToType<string>(dictionary["TheText"])?.Trim();
string文本= serializer.ConvertToType(dictionary[“TheText”])?.Trim();
Trim
将不起任何作用
如果序列化程序返回null
,您将在调用Trim
时得到NullReferenceException
您的代码最好是这样编写的(就初始化而言):
string theText =
((serializer.ConvertToType<string>(dictionary["TheText"])).Trim());
字符串文本=
((serializer.ConvertToType(dictionary[“TheText”])).Trim());
声明和初始化变量以及立即为其赋值没有意义
如果您不知道序列化程序可能会返回什么,则以下操作最安全:
string theText = ((serializer.ConvertToType<string>(dictionary["TheText"])));
if(!string.IsNullOrEmpty(theText))
{
theText = theText.Trim();
}
string theText=((serializer.ConvertToType(dictionary[“theText]”));
如果(!string.IsNullOrEmpty(文本))
{
theText=theText.Trim();
}
对空字符串调用Trim()
将导致空字符串。在null
上调用Trim()
public static class ExtensionMethods
{
public static string TrimIfNotNull(this string value)
{
if (value != null)
{
return value.Trim();
}
return null;
}
}
使用示例:
string concatenated = String.Format("{0} {1} {2}", myObject.fieldOne.TrimIfNotNull(), myObject.fieldTwo.TrimIfNotNull(), myObject.fieldThree.TrimIfNotNull());
正如一些评论中所建议的,您现在可以将c#6空条件运算符与以下语法一起使用:
string TheText = (serializer.ConvertToType<string>(dictionary["TheText"]))?.Trim();
string TheText=(serializer.ConvertToType(dictionary[“TheText”])?.Trim();
文档:您可以使用elvis操作符:
GetNullableString()?.Trim(); // returns NULL or trimmed string
您可以将此代码用作beblow
string theText = (((serializer.ConvertToType<string>(dictionary["TheText"])))+ string.Empty).Trim();
string theText=((serializer.ConvertToType(dictionary[“theText”]))+string.Empty.Trim();
在修剪之前,根据null
检查字符串的一些基本技巧:
(mystring???).Trim()
“空合并运算符”??
将返回第一个操作数。只有当此操作数为空时,才会返回第二个操作数(作为一种默认值)。
如果mystring为null,上面的示例将返回一个空字符串
mystring?.Trim()
“空条件运算符”?
将以点表示法缩短操作链。如果操作数为null,则不会执行以下操作并返回null。
如果mystring为null,上面的示例将返回null
if(string.IsNullOrWhiteSpace(mystring)){…}
如果您确实想检查mystring中是否有真实内容,则可以使用IsNullOrWhiteSpace()。如果操作数为null、空或只有空格字符,则返回true
您可以使用org.apache.commons.lang
StringUtils.trim(stringOrNull)
如果为null,它将崩溃…(myValue??)。trim()将一直工作。在C#6.0中,我们现在可以使用null条件运算符,如text?.trim()检查string.IsNullOrEmptyOrBlank
的最干净的方法是什么?@CodeBlend:你说的空白是什么意思?啊哈-如果你不得不修剪大量的字段,这是一件痛苦的事,如果字符串为空,是否有办法扩展trim方法以避免麻烦?@SelectDistinct-您可以编写一个扩展方法来实现这一点。@Oded Yeah很懒惰,为那些不知道Elvis含义的人发布供其他人参考?:这实际上被称为“安全导航操作符”。Elvis运算符是?:在C#中,它被称为空条件运算符:没有人关心它是如何被调用的,如果它工作,它就工作。问题是关于C#,而不是Java。