0

String.IsNullOrEmpty と String.IsNullorWhiteSpace の両方を組み合わせた .NET の組み込み関数はありますか?

私は自分で簡単に書くことができますが、私の質問は、なぜ String.IsNullOrEmptyOrWhiteSpace 関数がないのですか?

String.IsNullOrEmpty は最初に文字列をトリミングしますか? おそらくより良い質問は、String.Empty は空白とみなされますか? ということです。

4

4 に答える 4

13

String.IsNullOrEmptyOrWhiteSpace がないのはなぜですか

その関数は次のように呼ばれstring.IsNullOrWhiteSpaceます:

指定された文字列が null、空、または空白文字のみで構成されているかどうかを示します。

それは明らかだったはずではありませんか?

于 2011-04-15T16:28:15.963 に答える
0

はい、String.IsNullOrWhiteSpace方法です。

文字列がnull、空、または空白文字のみを含むかどうかをチェックするため、String.IsNullOrEmptyメソッドの機能が含まれます。

于 2011-04-15T16:29:42.817 に答える
0

String.IsNullOrWhiteSpace は、null、Empty、または WhiteSpace をチェックします。

これらのメソッドは、テストを実行する前に文字列を効果的にトリムするため、" " は true を返します。

于 2011-04-15T16:34:42.847 に答える
0

これは、dotPeek を使用した逆コンパイルされた方法です。

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
    public static bool IsNullOrEmpty(string value)
    {
      if (value != null)
        return value.Length == 0;
      else
        return true;
    }

    /// <summary>
    /// Indicates whether a specified string is null, empty, or consists only of white-space characters.
    /// </summary>
    /// 
    /// <returns>
    /// true if the <paramref name="value"/> parameter is null or <see cref="F:System.String.Empty"/>, or if <paramref name="value"/> consists exclusively of white-space characters.
    /// </returns>
    /// <param name="value">The string to test.</param>
    public static bool IsNullOrWhiteSpace(string value)
    {
      if (value == null)
        return true;
      for (int index = 0; index < value.Length; ++index)
      {
        if (!char.IsWhiteSpace(value[index]))
          return false;
      }
      return true;
    }
于 2012-02-17T19:22:19.983 に答える