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


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

    http://msdn.microsoft.com/en-us/library/ms171868(VS.100).aspx

    同时,还否定了一些原先的功能,这些过时的内容可以参考

    http://msdn.microsoft.com/en-us/library/ee461502%28VS.100%29.aspx

    下面就是.NET 4.0中新增加的String.IsNullOrWhiteSpace() 方法,方便用户对字符串进行处理。

    C# 代码
    using System;
    class TestNET4
    {
      
    staticvoid 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方法的具体实现代码为:

    C# 代码
    publicstaticbool IsNullOrWhiteSpace(string value)
    {
        
    if (value !=null)
        {
            
    for (int i =0; i < value.Length; i++)
            {
                
    if (!char.IsWhiteSpace(value[i]))
                {
                    
    returnfalse;
                }
            }
        }
        
    returntrue;
    }

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

  • 相关阅读:
    HDU-3622 Bomb Game 2sat
    HDU-4115 Eliminate the Conflict 2sat
    POJ-3678 Katu Puzzle 2sat
    [转]2-SAT问题及其算法
    Ros学习——roslaunch
    Ros学习调试——rqt_console
    Ros学习——创建程序包
    Ros学习service——小海龟
    Ros学习topic——小海龟
    旋转矩阵相关变换
  • 原文地址:https://www.cnblogs.com/goto/p/2831183.html
Copyright © 2020-2023  润新知