zoukankan      html  css  js  c++  java
  • C#中判断空字符串

    首先明确””,null和string.Empty的区别:

      string.Empty:不分配存储空间。

      "":分配一个长度为空的存储空间 ,""和String.Empty,这两个都是表示空字符串,空字符串是一个特殊的字符串,只不过这个字符串的值为空,在内存中是有准确的指向的。

      string.Empty就相当于"",一般用于字符串的初始化。比如: string a = string.Empty;在进行为空的比较时。string.Empty和""是一样的。即如果string test1 = "";则可以使用if(test1=="") 或者if(test1==string.Empty) 进行判断。上面两句是一样的效果。

      Null:null 关键字是表示不引用任何对象的空引用的文字值。null 是引用类型变量的默认值。那么也只有引用型的变量可以为NULL,如果 int i=null,的话,是不可以的,因为Int是值类型的。

      String.Empty和Null,这两个都是表示空字符串,string str1= String.Empty,这样定义后,str1是一个空字符串,空字符串是一个特殊的字符串,只不过这个字符串的值为空,在内存中是有准确的指向的 ,string str2=null,这样定义后,只是定义了一个string 类的引用,str2并没有指向任何地方,在使用前如果不实例化的话,都将报错。所以下面代码中执行test3.Length == 0就是错误的。

      判断空字符串: 

     string test1 = "";
      
    string test2 = string.Empty;
      
    string test3 = null;
      Response.Write(
    "test1 = \"\"" +" ");
      Response.Write(
    "test2 = string.Empty"  "</br>");
      Response.Write(
    "test3 = null" + "</br>");
      
    if (test1 == "")
      Response.Write(
    "(test1 == \"\") is :True"+"</br>");
      
    if(test2 == string.Empty)
      Response.Write(
    "(test2 == string.Empty) is:True" + "</br>");

      
    if(test1 == string.Empty)
      Response.Write(
    "(test1 == string.Empty) is: True" + "</br>");
      
    if(test2 == "")
      Response.Write(
    "(test2 == \"\") is: True" + "</br>");

      
    if(test1 == test2)
      Response.Write(
    "(test1 == test2) is: True" + "</br>");

      
    if(test3 == null)
      Response.Write(
    "(test3 == null) is: True" + "</br>");

      
    if (test1 != null)
      Response.Write(
    "(test1 != null) is : True" + "</br>");

      
    if (test2 != null)
      Response.Write(
    "(test2 != null) is : True" + "</br>");

      
    if(test1.Length ==0)
      Response.Write(
    "(test1.Length ==0) is: True" + "</br>");

      
    if(test2.Length==0)
      Response.Write(
    "(test2.Length==0) is : True" + "</br>");
      
    //if(test3.Length == 0)//Error,null不能用Length来进行判断为空
      if(string.IsNullOrEmpty(test1))

      Response.Write(
    "(string.IsNullOrEmpty(test1)) is :True" + "</br>");
      
    if (string.IsNullOrEmpty(test2))

      Response.Write(
    "(string.IsNullOrEmpty(test2)) is :True" + "</br>");
      
    if (string.IsNullOrEmpty(test3))

      Response.Write(
    "(string.IsNullOrEmpty(test3)) is :True" + "</br>");
      输出:

      test1
    = ""
      test2
    = string.Empty
      test3
    = null
      (test1
    == "") is :True
      (test2
    == string.Empty) is:True
      (test1
    == string.Empty) is: True
       (test2
    == "") is: True
      (test1
    == test2) is: True
      (test3
    == null) is: True
      (test1
    != null) is : True
      (test2
    != null) is : True
      (test1.Length
    ==0) is: True
      (test2.Length
    ==0) is : True
      (
    string.IsNullOrEmpty(test1)) is :True
      (
    string.IsNullOrEmpty(test2)) is :True
      (
    string.IsNullOrEmpty(test3)) is :True


      因此,判断字符串为空最通用的方法就是IsNullOrEmpty()无论是"", string.Empty还是null。如果字符串初始化为null,则不能使用test3.Length == 0进行判断。对于"",和string.Empty 使用s.Length == 0,s == string.Empty 和s == ""都可以,这里面不讨论性能问题。

  • 相关阅读:
    运用《深入理解Java虚拟机》书中知识解决实际问题
    FPGA实现移动目标检测
    FPGA实现人脸检测
    FPGA实现图像的边缘检测:灰度形态学梯度
    FPGA实现图像的二值形态学滤波:边界提取
    VAST3.0规范
    Flash Socket通信的安全策略问题 843端口
    100个开源游戏
    游戏指标分析
    网络广告类型有哪些?
  • 原文地址:https://www.cnblogs.com/tianguook/p/1686515.html
Copyright © 2011-2022 走看看