Empty 字段
折叠目录
展开目录

String.Empty 字段

表示空字符串。此字段为只读。

命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)

public static readonly string Empty
public static final String Empty
public static final var Empty : String

此字段的值为零长度字符串 ""。

下面的代码示例演示如何使用 Empty 字段。

在第一个示例中,如果另一个字段的值为 空引用(在 Visual Basic 中为 Nothing),则 Empty 字符串作为默认值返回。

DataBinding myBinding = DataBindings["Text"];
if (myBinding != null)
{
   return myBinding.Expression;
}
return String.Empty;

DataBinding myBinding = get_DataBindings().get_Item("Text");
if (myBinding != null) {
    return myBinding.get_Expression();
}
return("");

在第二个示例中,Compare 中使用了 Empty 字符串来测试子字符串。

String myString = "abc";
bool test1 = String.Compare(myString.Substring(2, 1), "c") == 0; // This is true.
myString.Substring(3, 1); // This throws ArgumentOutOfRangeException.
bool test2 = String.Compare(myString.Substring(3, 0), String.Empty) == 0; // This is true.

String myString = "abc";
// This is true.
boolean test1 = String.Compare(myString.Substring(2, 1), "c") == 0;        
myString.Substring(3, 1); // This throws ArgumentOutOfRangeException.
// This is true.
boolean test2 = String.Compare(myString.Substring(3, 0), " ") == 0;        

var myString : String = "abc";
var test1 : boolean = String.Compare(myString.Substring(2, 1), "c") == 0; // This is true.
myString.Substring(3, 1);  // This throws ArgumentOutOfRangeException.
var test2 : boolean = String.Compare(myString.Substring(3, 0), String.Empty) == 0; // This is true.

在第三个示例中,XPathNavigator 对象的决策块中使用了 Empty 字符串来作出有关 XML 分析的决策。

public static void RecursiveWalk(XPathNavigator nav)
{
   switch (nav.NodeType){
     case XPathNodeType.Element:
        if (nav.Prefix==String.Empty)
          Console.WriteLine("<{0}>", nav.LocalName);
        else
          Console.Write("<{0}:{1}>", nav.Prefix, nav.LocalName);
          Console.WriteLine("\t"+ nav.NamespaceURI);
        break;
     case XPathNodeType.Text:
        Console.WriteLine("\t" + nav.Value);
        break;
    }

    if ( nav.MoveToFirstChild() )
   {
      do{
         RecursiveWalk(nav);
      } while ( nav.MoveToNext() );

      nav.MoveToParent();
      if (nav.NodeType == XPathNodeType.Element)
        Console.WriteLine("</{0}>", nav.Name);
    }
}	

public static void RecursiveWalk(XPathNavigator nav)
{
    switch (nav.get_NodeType()) {
        case XPathNodeType.Element:
            if (nav.get_Prefix().Equals("")){
                Console.WriteLine("<{0}>", nav.get_LocalName());
            }
            else {
                Console.Write("<{0}:{1}>", nav.get_Prefix(),
                    nav.get_LocalName());
            }
            Console.WriteLine("\t" + nav.get_NamespaceURI());
            break;

        case XPathNodeType.Text:
            Console.WriteLine("\t" + nav.get_Value());
            break;
    }

    if (nav.MoveToFirstChild()) {
        do {
            RecursiveWalk(nav);
        } while (nav.MoveToNext());

        nav.MoveToParent();
        if (nav.get_NodeType() .Equals(XPathNodeType.Element)) {
            Console.WriteLine("</{0}>", nav.get_Name());
        }
    }
} //RecursiveWalk

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

社区附加资源

添加
显示:
© 2016 Microsoft