是否使用string. isnullorempty (string)检查字符串时,认为有string. isnullowhitespace (string)在。net 4.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;
}
其他回答
下面是这两个方法的实际实现(使用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;
}
string. isnullorempty (str) -如果你想检查字符串值是否已提供
string.IsNullOrWhiteSpace(str)——基本上这已经是一种业务逻辑实现(例如,为什么“”是坏的,但像“~~”这样的东西是好的)。
我的建议是——不要将业务逻辑与技术检查混为一谈。 例如,字符串。IsNullOrEmpty最适合在方法的开头使用,以检查其输入参数。
在.Net标准2.0中:
string. isnullorempty():指定的字符串是空字符串还是空字符串。
Console.WriteLine(string.IsNullOrEmpty(null)); // True
Console.WriteLine(string.IsNullOrEmpty("")); // True
Console.WriteLine(string.IsNullOrEmpty(" ")); // False
Console.WriteLine(string.IsNullOrEmpty(" ")); // False
string. isnullorwhitespace():指定的字符串是否为空、空或仅由空白字符组成。
Console.WriteLine(string.IsNullOrWhiteSpace(null)); // True
Console.WriteLine(string.IsNullOrWhiteSpace("")); // True
Console.WriteLine(string.IsNullOrWhiteSpace(" ")); // True
Console.WriteLine(string.IsNullOrWhiteSpace(" ")); // True
这个怎么样?
if (string.IsNullOrEmpty(x.Trim())
{
}
这将修剪所有的空格,如果它们在那里,避免IsWhiteSpace的性能损失,这将使字符串满足“空”条件,如果它不是空的。
我也认为这是更清楚的,它通常是一个很好的实践,修剪字符串,特别是如果你把它们放入数据库或其他东西。
用IsNullOrEmpty和IsNullOrwhiteSpace检查一下
string sTestes = "I like sweat peaches";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < 5000000; i++)
{
for (int z = 0; z < 500; z++)
{
var x = string.IsNullOrEmpty(sTestes);// OR string.IsNullOrWhiteSpace
}
}
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);
Console.ReadLine();
您将看到IsNullOrWhiteSpace要慢得多:/
推荐文章
- 如何将linq结果转换为哈希集或哈希集
- 通用 TryParse
- 'useLegacyV2RuntimeActivationPolicy'在.NET 4配置中做什么?
- HTTP POST返回错误:417“期望失败。”
- 如何在。net中创建和使用资源
- 为什么Path。以Path.DirectorySeparatorChar开头的文件名合并不正确?
- 如何在c#中获得正确的时间戳
- Linq选择列表中存在的对象(A,B,C)
- c# .NET中的App.config是什么?如何使用它?
- c#:如何获得一个字符串的第一个字符?
- String类中的什么方法只返回前N个字符?
- 我如何提高ASP。NET MVC应用程序性能?
- 更好的方法将对象转换为int类型
- 我可以将c#字符串值转换为转义字符串文字吗?
- 在c#中转换char到int