【问题标题】:Concatenate two strings and get null if both are null连接两个字符串,如果两者都为空,则为空
【发布时间】:2021-09-26 12:48:18
【问题描述】:

我正在寻找一种解决方案来连接两个字符串值并得到null,如果两者都是nullstring1 + string2string.Concat(string1, string2)string.Join(string1, string2) 都不起作用。研究表明,这是因为这些方法在内部将 null 视为空字符串。

如何解决?

【问题讨论】:

  • 你为什么不事先检查string1 == null && string2 == null
  • string result = string1 == null && string2 == null ? null : string1 + string2;
  • 如果我最终的目标是更复杂的公式:(a + b) ?? (c + d),但是当ab 都是nulla + b 不会计算为null
  • string result = (a == null && b == null) ? c + d : a + b; 如果ab 都是null,我们应该计算c + d,否则a + b

标签: c# .net string concatenation


【解决方案1】:

因为你的实际公式是

(a + b) ?? (c + d) // assumes that null + null == null in (a + b)

建议改写成

a == null && b == null ? c + d : a + b

提供预期结果:

  • 如果 两者 abnull 我们有 c + d
  • a + b 否则

如果你想在所有a, b, c, d 都是null 时拥有null(不是空字符串):

a == null && b == null ? 
  c == null && d == null 
    ? null 
    : c + d 
    : a + b;

【讨论】:

  • 解决XY问题,我喜欢。
【解决方案2】:

这样的?

public class StringTest
{
    public string CustomConcat(string one, string two) => 
        one == null && two == null 
            ? null 
            : string.Concat(one, two);

    [Test]
    public void ConcatTest()
    {
        Assert.IsNull(CustomConcat(null, null));
        Assert.AreEqual("one", CustomConcat("one", null));
        Assert.AreEqual("two", CustomConcat(null, "two"));
        Assert.AreEqual("onetwo", CustomConcat("one", "two"));

        // finally, a test for (a + b) ?? (c + d)
        Assert.AreEqual("threefour", CustomConcat(null, null) ?? CustomConcat("three", "four"));
    }
}

【讨论】:

    【解决方案3】:

    是的,不同的方法调用处理不同的前置条件,但您始终可以创建自己的函数来处理“自定义”前置条件。

    作为最简单的选择,您可以尝试以下方法:

    String concatenateStrings(string s1, string s1) {
    return (s1 == null && s2 == null)? null : String.concat(s1,s2);
    }
    

    【讨论】:

    • 请在发布答案之前阅读已经添加的答案,并且请避免添加与其他答案非常非常相似的答案。 BR – 咆哮 S.
    【解决方案4】:

    你可以这样做:

                var textResult = string.Empty;
    
                if (string.IsNullOrWhiteSpace(text1) && 
    string.IsNullOrWhiteSpace(text2))
                {
                    textResult = null;
                }
                else
                    if (!string.IsNullOrWhiteSpace(text1) && 
    string.IsNullOrWhiteSpace(text2))
                    {
                        textResult = text1;
                    }
                    else
                        if (string.IsNullOrWhiteSpace(text1) && 
    !string.IsNullOrWhiteSpace(text2))
                        {
                            textResult = text2;
                        }
    
                textResult = $"{text1} {text2}";
    

    或者以更好的方式:

            textResult = (!string.IsNullOrWhiteSpace(text1) && 
    string.IsNullOrWhiteSpace(text2)) ?
                text1 :
                    (string.IsNullOrWhiteSpace(text1) && 
    !string.IsNullOrWhiteSpace(text2)) ?
                            text2 :
                            (string.IsNullOrWhiteSpace(text1) && 
    !string.IsNullOrWhiteSpace(text2)) ?
                            text2 :
                            $"{text1} {text2}";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 2016-12-30
      • 1970-01-01
      • 2021-12-10
      • 2015-07-16
      • 2018-06-22
      相关资源
      最近更新 更多