static void Main(string[] args)
{
string string1 = " "; // it can be NULL and any word too
string string2 = null; // it can be NULL and any word too
if (string.IsNullOrEmpty(string1))
{
if (string.IsNullOrEmpty(string2))
{
Console.WriteLine("Both the string Null & equal");
}
}
else
{
if (!string.IsNullOrEmpty(string2))
{
if(string1.Trim().Equals(string2.Trim(),StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Both the string has value& equal");
}
}
}
}
此代码最终检查两个字符串的 NULL 或值,以确认两个字符串相同。重要的是,它必须修剪空格以使其具有可比性,同时如果字符串为 NULL,则无法修剪。
经历了所有可能的条件,我已经编写了这段代码,并且仍然相信有人可以在效率方面提高效率。
谢谢!
假设您真的想检查 null 而不是 null 或空(根据您的控制台评论),我将实现以下方法......
private bool checkEqualityWithTrim(string string1, string string2)
{
bool bothNull = string1 == null && string2 == null;
bool bothNonNullAndEqualTrimmed = string1 != null && string2 != null && string1.Trim() == string2.Trim();
return bothNull || bothNonNullAndEqualTrimmed;
}
然后你可以做...
var areEqual = checkEqualityWithTrim(string1, string2);
如果 IsNullOrEmpty() 是有意为之的,则只需将 bothNull 行替换为
bool bothNull = string.IsNullOrEmpty(string1) && string.IsNullOrEmpty(string2);
是的,您正在执行比需要做的更多的检查。如果字符串相等,则只需检查其中一个字符串是否为空或空格。如果是这样,您知道两个字符串中的值。假设对您来说 NULL 和空格是等效的,这是有效的。
public static void Main(string[] args)
{
string string1 = ""; // it can be NULL and any word too
string string2 = ""; // it can be NULL and any word too
if (String.Equals((string1 ?? "").Trim(), (string2 ?? "").Trim(),StringComparison.OrdinalIgnoreCase))
{
if (string.IsNullOrEmpty(string1)) //since the strings are equal, check one of the strings
{
Console.WriteLine("Both strings are null or empty & equal");
}
else
{
Console.WriteLine("Both strings have values & are equal");
}
}
}
这是我对你的尝试。
如果这是要经常使用的东西,那么也许使用扩展方法可能是要走的路。
我为您创建了两种扩展方法。
1 执行null
和whitespace
检查(这两个条件都将被视为null
第二个执行你所追求的逻辑。
这是我对你的尝试:
public static bool IsNull(this string source)
{
return string.IsNullOrWhiteSpace(source);
}
public static string IsNullOrSame(this string source, string comparer)
{
//check for both values are null
if (source.IsNull() && comparer.IsNull())
{
return "Both Values Null or contain whitespace only";
}
//check if string 1 is null and string two has a value.
if (source.IsNull() && !comparer.IsNull())
{
return "I don't have a Value, but string 2 does";
}
//check if string 1 has a value and string two is null
if (!source.IsNull() && comparer.IsNull())
{
return "I have Value, but string 2 doesn't";
}
//if we make it this far then both have a value, check if the string value matches
if(source.Trim().Equals(comparer.Trim(), StringComparison.OrdinalIgnoreCase))
{
return "Both value match";
}
//if values don't match
return "strings both have values but don't match";
}
将这些扩展方法包含在项目中后,您可以执行一些简单操作,例如:
var string1 = "some value";
var string2 = null;
var result = string1.IsNullOrSame(string2);
Console.WriteLine(result);
这将导致消息"I have Value, but string 2 doesn't"
多个返回语句的原因纯粹是为了可读性。如果我们满足一个"条件",那么就没有必要再执行任何检查,并且多个 if 的嵌套可能会有点烦人的调试。
希望这能为您提供所需的功能和效率。
您正在寻找简单且可维护的代码而不是效率...
我会这样编码:
(编辑:现在具有所有可能的条件)
{
String string1 = "";
String string2 = "";
if (String.IsNullOrEmpty(string1.Trim()) && String.IsNullOrEmpty(string2.Trim()))
{
Console.WriteLine("Both the string Null & equal");
}
else if (!String.IsNullOrEmpty(string1.Trim()) && String.IsNullOrEmpty(string2.Trim()))
{
Console.WriteLine("String2 is null and string1 is not!");
}
else if (String.IsNullOrEmpty(string1.Trim()) && !String.IsNullOrEmpty(string2.Trim()))
{
Console.WriteLine("String1 is null and string2 is not!");
}
else {
if (string1.Trim().Equals( string2.Trim())) {
Console.WriteLine("both strings are not null and Equals!");
}
else {
Console.WriteLine("both strings are not null! and not Equals");
}
}
}
如果你可以使用 C# 6,我肯定会建议你使用 Null 条件运算符(也称为 Elvis 运算符):
var test = "";
var test2 = "";
if (String.IsNullOrEmpty(test?.Trim()) && String.IsNullOrEmpty(test2?.Trim()))
{
Console.WriteLine("both strings are null or empty and equal");
}
else
{
Console.WriteLine("both strings have value and are equal");
}
此外,根据字符串" "
(空格)对您(空或值)的含义,使用IsNullOrEmpty
(在值的情况下)或IsNullOrWhitespace
(在空的情况下)。