String.IsNullOrEmpty と String.IsNullorWhiteSpace の両方を組み合わせた .NET の組み込み関数はありますか?
私は自分で簡単に書くことができますが、私の質問は、なぜ String.IsNullOrEmptyOrWhiteSpace 関数がないのですか?
String.IsNullOrEmpty は最初に文字列をトリミングしますか? おそらくより良い質問は、String.Empty は空白とみなされますか? ということです。
String.IsNullOrEmpty と String.IsNullorWhiteSpace の両方を組み合わせた .NET の組み込み関数はありますか?
私は自分で簡単に書くことができますが、私の質問は、なぜ String.IsNullOrEmptyOrWhiteSpace 関数がないのですか?
String.IsNullOrEmpty は最初に文字列をトリミングしますか? おそらくより良い質問は、String.Empty は空白とみなされますか? ということです。
String.IsNullOrEmptyOrWhiteSpace がないのはなぜですか
その関数は次のように呼ばれstring.IsNullOrWhiteSpace
ます:
指定された文字列が null、空、または空白文字のみで構成されているかどうかを示します。
それは明らかだったはずではありませんか?
はい、String.IsNullOrWhiteSpace
方法です。
文字列がnull、空、または空白文字のみを含むかどうかをチェックするため、String.IsNullOrEmpty
メソッドの機能が含まれます。
String.IsNullOrWhiteSpace は、null、Empty、または WhiteSpace をチェックします。
これらのメソッドは、テストを実行する前に文字列を効果的にトリムするため、" " は true を返します。
これは、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;
}