.NET4.0新功能之String.IsNullOrWhiteSpace() 方法

在.NET Framework 4.0 中新增加了一些很方便的功能,比如:System.Dynamic支持动态语言运行时、System.Numerics.Complex 复数、System.Numerics.BigInteger 大数、System.Tuple 对象、遍历文件夹下文件夹和文件的新方法Directory.EnumerateDirectories、Directory.EnumerateFiles、Directory.EnumerateFileSystemEntries等,详细的新功能列表可以参考下面的链接:

[url]http://msdn.microsoft.com/en-us/library/ms171868(VS.100).aspx[/url]
同时,还否定了一些原先的功能,这些过时的内容可以参考

[url]http://msdn.microsoft.com/en-us/library/ee461502%28VS.100%29.aspx[/url]
下面就是.NET 4.0中新增加的String.IsNullOrWhiteSpace() 方法,方便用户对字符串进行处理。

using System;
class TestNET4
{
static void Main()
{
String[] TestString = { null, String.Empty, " ", " ", "abc ", "\t", "\r\n", "\v", "\f", "\a" };
for (int i = 0; i < TestString.Length; i++)
{
String temp = TestString[i];
if (temp == null)
{
Console.WriteLine(" null IsNullOrWhiteSpace = "
+ String.IsNullOrWhiteSpace(temp).ToString());
}
else
{
Console.WriteLine(temp + " Length=" + temp.Length.ToString()
+ " IsNullOrWhiteSpace = "
+ String.IsNullOrWhiteSpace(temp).ToString());
}
}
}
}

程序执行结果:

 null IsNullOrWhiteSpace = True
Length=0 IsNullOrWhiteSpace = True
Length=1 IsNullOrWhiteSpace = True
  Length=1 IsNullOrWhiteSpace = True
abc Length=4 IsNullOrWhiteSpace = False
Length=1 IsNullOrWhiteSpace = True

Length=2 IsNullOrWhiteSpace = True
Length=1 IsNullOrWhiteSpace = True
Length=1 IsNullOrWhiteSpace = True
Length=1 IsNullOrWhiteSpace = False

IsNullOrWhiteSpace方法的具体实现代码为:


public static bool IsNullOrWhiteSpace(string value)
{
if (value != null)
{
for (int i = 0; i < value.Length; i++)
{
if (!char.IsWhiteSpace(value[i]))
{
return false;
}
}
}
return true;
}

所以,他是通过判断char.IsWhiteSpace方法来实现的,有些特殊字符也被当作空白字符,这一点特别注意注意,比如全角空格。


本文来自CSDN博客,转载请标明出处:
[url]http://blog.csdn.net/net_lover/archive/2009/11/02/4757383.aspx[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值