zoukankan      html  css  js  c++  java
  • string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别

    string.IsNullOrEmpty

    都知道,这个功能是判断字符串是否为:null或者string.Empty。如果是如"\t"这样的字符就返回false了,为了达到判断过滤这些功能,就要使用Trim()和Length属性帮忙,判断是否长度为零,于是乎就产生了如下的方法。

    string.IsNullOrWhiteSpace

    这个是判断所有空白字符,功能相当于string.IsNullOrEmpty和str.Trim().Length总和,他将字符串给Char.IsWhiteSpace为ture的任何字符都将是正确的。根据MSDN的说明,这个方法会比调用上述两个方法的性能更高而且简洁,所以在判断这个功能时,推荐使用。

    using System;
    
    public class Example
    {
       public static void Main()
       {
          string[] values = { null, String.Empty, "ABCDE", 
                              new String(' ', 20), "  \t   ", 
                              new String('\u2000', 10) };
          foreach (string value in values)
             Console.WriteLine(String.IsNullOrWhiteSpace(value));
       }
    }
    // The example displays the following output:
    //       True
    //       True
    //       False
    //       True
    //       True
    //       True
  • 相关阅读:
    宠物小精灵之收服(dp)
    装箱问题(dp)
    背包问题(采药)
    导弹防御系统(dfs+贪心)
    2018091-2 博客作业
    第一周例行报告
    洛谷 P6378 [PA2010]Riddle
    洛谷 P5782 [POI2001]和平委员会
    洛谷 P3825 [NOI2017]游戏
    POJ 3683 Priest John's Busiest Day
  • 原文地址:https://www.cnblogs.com/zyj649261718/p/4972976.html
Copyright © 2011-2022 走看看